繁体   English   中英

WPF-使用C#从WPF中的Mysql数据库检索(选择或下载)图像

[英]WPF - Retrieve (Select OR Download) Image from Mysql Database in WPF using C#

我可以通过这段代码从WPF中的WPF中的Mysql数据库下载(检索)图像。 我从https://www.experts-exchange.com/questions/25096053/Retrieve-images-in-C-WPF-Application-from-SQL-Server-Database.html网站复制了此代码。 但是我不知道这段代码是如何逐行工作的。 如果有人对此有所了解,请提供帮助。

代码在这里。

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; 
            }

我能够使用此代码从c#中的wpf数据库中检索图像。 此代码的唯一问题是它一次只能检索一个图像。 在使用此代码之前,请添加System.Drawing.Imaging。 代码中的库。

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM