Warm tip: This article is reproduced from stackoverflow.com, please click
c# wpf

Why i cant Use FromStream type in type Image?

发布于 2020-04-07 10:23:24

I had referenced to System.Drawing But i Cant use FromStream(...) in my code and also i get this error from VS the type name FromStream does not exist in the type Image

For More help

           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;

        }
Questioner
Efijoon
Viewed
54
Clemens 2020-02-01 03:26

Your code could be significantly simplified. There is no need to use anything from the WinForms namespace System.Drawing at all.

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