Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, November 7, 2020 9:38 PM
Hallo,
I have a dataGridView populated with data from an MS SQL database. The database contains images with varbinary(MAX) type. I have write the following code that can read the path of the image that is saved to hard disk when you double click in the dataGridView and show the image to a pictureBox.
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.CurrentRow.Selected = true;
int cardNo = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["cardNoDataGridViewTextBoxColumn"].FormattedValue);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT ImageFull FROM SS1 WHERE CardNo = '" + cardNo + "'", con);
string img = cmd.ExecuteScalar().ToString();
pictureBox1.Image = Image.FromFile(img);
con.Close();
}
I want to double click in the dataGridView and reads the image direct from the database and show it to a pictureBox.
Could you please help me with this?
All replies (3)
Monday, November 9, 2020 3:13 AM âś…Answered | 1 vote
Hi Babis,
Thank you for posting here.
Since there is a column of type varbinary (MAX) in your database table for storing images, after reading it into the datagridview, you can get the byte array in simple steps, and then you can use Karen's code or the following code to convert it into an Image object.
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
Byte[] bytes = (byte[])dataGridView1.Rows[e.RowIndex].Cells["image"].Value;
using (var ms = new MemoryStream(bytes))
{
pictureBox1.Image = Image.FromStream(ms);
}
}
Best Regards,
Timon
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Sunday, November 8, 2020 12:04 AM | 2 votes
Hello,
In the following code sample I read data into a DataGridView then on double clicking a row read an image from a database table which is done with Entity Framework Core where the image comes back as a byte array. U
sing a SqlConnection + SqlCommand e.g. we have SQL tested in SSMS that when used in C# code @Id is a parameter for the SqlCommand object.
DECLARE @Id AS int = 1
SELECT Picture FROM dbo.Categories WHERE dbo.Categories.CategoryID = @Id;
In both cases we have a byte array which converts to an Image via
public static Image ByteArrayToImage(byte[] contents)
{
if (contents is null)
{
return InvalidImage(); // or return null
}
else
{
var converter = new ImageConverter();
var image = (Image)converter.ConvertFrom(contents);
return image;
}
}
Table structure, note the image column named Picture type is Image.
Finally if you want to try the code here is the database script. Full source code.
If you never looked at Entity Framework Core you might be surprised how easy it is to use.
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
My GitHub code samples
GitHub page
Monday, November 9, 2020 6:40 PM
Note in my code sample I used type image which is why the method below was used.
public static Image ByteArrayToImage(byte[] contents)
{
if (contents is null)
{
return null
}
else
{
var converter = new ImageConverter();
var image = (Image)converter.ConvertFrom(contents);
return image;
}
}

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
My GitHub code samples
GitHub page