温馨提示:本文翻译自stackoverflow.com,查看原文请点击:c# - Why i cant Use FromStream type in type Image?
c# wpf

c# - 为什么我不能在Image类型中使用FromStream类型?

发布于 2020-04-09 10:34:34

我已经引用了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;

        }

查看更多

提问者
Efijoon
被浏览
105
Clemens 2020-02-01 03:26

您的代码可以大大简化。根本不需要使用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;
    }
}