How to Retrieve and Display Images from MySQL Database
Retrieving images from a MySQL database into a PictureBox control requires a specific approach with proper byte array handling. The following steps demonstrate the process:
Insert Image into MySQL Database
Create a MySqlCommand and insert the byte array into the database using a parameter:
cmd.Parameters.AddWithValue("@image", bytes); cmd.ExecuteNonQuery();
Retrieve Image from MySQL Database
Execute a query with a parameter to retrieve the image:
cmd.Parameters.AddWithValue("@id", Properties.Settings.Default.idImg); MySqlDataReader row; row = cmd.ExecuteReader();
Read the image byte array from the row:
while (row.Read()) { ImageByte = (Byte[])(row["image"]); }
Convert Byte Array to Image and Display
Convert the byte array to an Image using the Helper.ByteArrayToImage method:
roundPictureBox1.Image = byteArrayToImage(ImageByte); roundPictureBox1.Refresh();
Enhancements
The above is the detailed content of How to Load and Display Images from a MySQL Database in C#?. For more information, please follow other related articles on the PHP Chinese website!