Reputation: 1360
I have datagrid view with a column type of image. I want to get the value from the datagrid to the picture box that I'm using. Here's my code located in my CellClick
byte[] image = (byte[])dgv_salesquotesummary.Rows[e.RowIndex].Cells[7].Value;
MemoryStream ms = new MemoryStream(image);
pb_productImage.Image = Image.FromStream(ms);
I got this error that says
Additional information: Unable to cast object of type 'System.Drawing.Bitmap' to type 'System.Byte[]'.
Thank you in advance!
Upvotes: 1
Views: 449
Reputation: 5580
The error message shows a failure on casting Bitmap to byte[]. This means the cell Value is a Bitmap, which derive from Image. In simple types usually you can just use it immediately like this :
pb_productImage.Image = (Image) dgv_salesquotesummary.Rows[e.RowIndex].Cells[7].Value
However, since in this case the Image could be disposed at any time by your datagrid, it's prudent to clone the Image first, like :
pb_productImage.Image = (Image)(((Bitmap)dgv_salesquotesummary.Rows[e.RowIndex].Cells[7].Value).Clone())
Upvotes: 2