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
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3