I am able to download (retrieve) image from Mysql Database in WPF in C# by this piece of code. This code i have copy from this https://www.experts-exchange.com/questions/25096053/Retrieve-images-in-C-WPF-Application-from-SQL-Server-Database.html Website. But i don't know how this code work line by line. If anyone who have knowledge about this please help.
Code is here.
string query = "SELECT image_data from image_table WHERE image_id=22";
MySqlCommand cmd = new MySqlCommand(query, connection);
MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
Byte[] bindata = (Byte[])dataReader["image_data"];
MemoryStream strm = new MemoryStream();
strm.Write(bindata, 0, bindata.Length);
strm.Position = 0;
System.Drawing.Image img = System.Drawing.Image.FromStream(strm);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
bi.StreamSource = ms;
bi.EndInit();
download.Source = bi;
}
i am able to retrieve image from database in wpf in c# with this code. The only problem with this code is it retrieve only one image ata a time. Before using this code add System.Drawing.Imaging. library in your code.
BitmapImage bi = new BitmapImage();
System.Drawing.Image img;
MemoryStream strm = new MemoryStream();
strm.Write(bindata, 0, bindata.Length);
strm.Position = 0;
img = System.Drawing.Image.FromStream(strm);
bi.BeginInit();
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Seek(0, SeekOrigin.Begin);
bi.StreamSource = ms;
bi.EndInit();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.