wpf—使用c从wpf中的mysql数据库检索(选择或下载)图像#

ou6hu8tu  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(350)

通过这段代码,我可以从c的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; 
            }
cxfofazt

cxfofazt1#

我可以用这段代码从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();

相关问题