I am able to save an image from a picture box into the database of SQL. I used a byte array and filestream to read the image.

FileStream fs = new FileStream(imagename, FileMode.Open, FileAccess.Read)
byte[] picbyte = new byte[fs.Length]
fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length))
fs.Close()

For some reason, I can't retrieve the image into a dataset to datagridview. Also to a picturebox. Need help

Recommended Answers

All 4 Replies

Take a look at this code I made some days ago. It gets the images from DB into ImageList:

List<ImageList> list;

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            list = new List<ImageList>();
            using (SqlConnection sqlConn = new SqlConnection("connString"))
            {
                string query = String.Format(@"SELECT PictureName, MyImage FROM Images");
                SqlCommand cmd = new SqlCommand(query, sqlConn);
                cmd.Connection.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        ImageList _image = new ImageList();
                        _image.imageName = (string)reader[0];
                        byte[] data = (byte[])reader[1];
                        using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
                        {
                            Image image = new Bitmap(ms);
                            _image.imagePicture = image;
                            list.Add(_image);
                        }
                    }
                }
            }
            if (list.Count == 0)
                MessageBox.Show("There are no pictures in the database.");
        }

Mitja

I successfully binded the image to a dataset using this

byte[] picbyte = ((byte[])rdr["visitorImage"]);

                        FileStream fs = new FileStream("image.bmp", FileMode.Create);
                        fs.Write(picbyte, 0, picbyte.Length);

                        displaypb.Image = Image.FromStream(fs);

How could I do this with datagridview? When setting the column properties manually I get a row value System.Byte[]. How to read the bytes to store it to datagridview.

Instead of using DataGridViewImageColumn use DataGridViewTextBoxColumn with ReadOnly property set to true. When you wish to show an image in the cell use following code:

DataGridViewImageCell iCell = new DataGridViewImageCell();
iCell .Value = myImage; //image value
dataGridView1[columnIndex, rowIndex] = iCell;

.. or:
http://www.daniweb.com/software-development/csharp/threads/339535

What would be my image value?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.