C# - How To Add Image From pictureBox To Datagridview In C#

c# Add Image From pictureBox To Datagridview

C# - How To Insert Image From pictureBox To Datagridview In C#

__________________________________________________________________________

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


Project Source Code:

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

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

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

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

            dataGridView1.Columns.Add(dgvId);
            dataGridView1.Columns.Add(dgvImage);

            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.RowTemplate.Height = 120;

            dataGridView1.AllowUserToAddRows = false;

        }

        //browse image into pictureBox1 
        private void BtnBrowseImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog();
            opf.Filter = "Choose Image(*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif";

            if(opf.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(opf.FileName);
            }
        }

        //button add data to dataGridView
        //insert image from pictureBox to dataGridView 
        private void BtnAdd_Click(object sender, EventArgs e)
        {

            
            try
            {
                MemoryStream ms = new MemoryStream();
                pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
                byte[] img = ms.ToArray();
                dataGridView1.Rows.Add(textBoxId.Text, img);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
            
        }
    }
}
///////////////OUTPUT:
Add Image From pictureBox To Datagridview
Add Image From pictureBox To Datagridview




Share this

Related Posts

Previous
Next Post »

1 comments:

comments
Anonyme
30 mars 2019 à 12:08 delete

Mis Lineas --

MemoryStream ms = new MemoryStream();
N.Image.Save(ms, N.Image.RawFormat);
byte[] img = ms.ToArray();
//dataGridView1.Rows.Add(textBoxId.Text, img);
obpre.Rows[e.RowIndex].Cells[0].Value = img;

Me marca que no puede convertirse de un valor String a un System.drawing.imagen.
Ocupo colocar la imagen de un picturebox en una columna de un datagried

Reply
avatar