我已经引用了System.Drawing但我无法在代码中使用FromStream(...),而且我从VS中收到此错误,类型Image中不存在类型名称FromStream
寻求更多帮助
if (Resualt[0].ProductImage != null)
{
byte[] ImageArray = (byte[])Resualt[0].ProductImage;
MemoryStream stream = new MemoryStream();
stream.Write(ImageArray, 0, ImageArray.Length);
System.Drawing.Image Img = System.Drawing.Image.FromStream(stream);
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();
ImgProduct.Source = Bi;
}
您的代码可以大大简化。根本不需要使用WinForms命名空间System.Drawing
中的任何内容。
var buffer = Result[0].ProductImage as byte[]; // note Result instead of Resualt
if (buffer != null)
{
using (var stream = new MemoryStream(buffer))
{
var bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.StreamSource = stream;
bi.EndInit();
ImgProduct.Source = bi;
}
}