C# - How To Get Image From dataGridView To pictureBox In C#

C# pictureBox Image To Datagridview

C# - How To Display Image From dataGridView To pictureBox In C#

__________________________________________________________________________

In This C# Tutorial We Will See How To  Get Picture From DataGridView To PictureBox In CSharp Programming Language .


Project Source Code:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class CSharp_Get_Image_From_Datagridview_To_PictureBox : Form
    {
        public CSharp_Get_Image_From_Datagridview_To_PictureBox()
        {
            InitializeComponent();
        }

        private void CSharp_Get_Image_From_Datagridview_To_PictureBox_Load(object sender, EventArgs e)
        {
            //create a DataGridView Image Column
            DataGridViewImageColumn dgvImageColumn = new DataGridViewImageColumn();
            //set a header text to DataGridView Image Column
            dgvImageColumn.HeaderText = "Image";
            dgvImageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch;

            DataGridViewTextBoxColumn dgvIdColumn = new DataGridViewTextBoxColumn();
            dgvIdColumn.HeaderText = "Id";

            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            dataGridView1.RowTemplate.Height = 130;

            dataGridView1.AllowUserToAddRows = false;

            // add columns to datagridview
            dataGridView1.Columns.Add(dgvIdColumn);
            dataGridView1.Columns.Add(dgvImageColumn);

            // populate the datagridview with some message
            Image img1;
            img1 = Image.FromFile(@"D:\Images\img1.jpg");
            dataGridView1.Rows.Add("RTUIKJGRR", img1);

            Image img2;
            img2 = Image.FromFile(@"D:\Images\img2.jpg");
            dataGridView1.Rows.Add("AZERTYU", img2);

            Image img3;
            img3 = Image.FromFile(@"D:\Images\img3.jpg");
            dataGridView1.Rows.Add("MLKJHG", img3);

            Image img4;
            img4 = Image.FromFile(@"D:\Images\img4.jpg");
            dataGridView1.Rows.Add("FGHJKLKUY", img4);

            Image img5;
            img5 = Image.FromFile(@"D:\Images\img5.jpg");
            dataGridView1.Rows.Add("VBJHGF", img5);
        }

        // display image into picturebox in datagridview cell click
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            textBoxId.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();

            MemoryStream ms = new MemoryStream();
            Bitmap img = (Bitmap)dataGridView1.CurrentRow.Cells[1].Value;
            img.Save(ms, ImageFormat.Jpeg);
            pictureBox1.Image = Image.FromStream(ms);
        }
    }
}

///////////////OUTPUT:
c# pictureBox Image To Datagridview
pictureBox Image To Datagridview In C#





Share this

Related Posts

Previous
Next Post »

2 comments

comments
5 novembre 2015 à 03:51 delete

please can you tell me how to save multiple chechbox in database when a they are checked

Reply
avatar
6 mai 2019 à 23:51 delete

please can you tell me hwo to Get a image in data gridview to picturebox in asp.net using c#

Reply
avatar