How to convert from type Image to type BitmapImage?

后端 未结 3 1714
北海茫月
北海茫月 2020-12-31 23:55

Does anyone know how to create a BitmapImage from an Image? Here is the code that I\'m working with:

MemoryStream memStream = new MemoryStream(bytes);
Image          


        
相关标签:
3条回答
  • 2020-12-31 23:57

    Are you sure you need BitmapImage, not BitmapSource? BitmapImage is a subclass of BitmapSource specifically for making a source in XAML (from a URI). There are many other BitmapSource choices -- one of the other choices might be better.

    For example, WriteableBitmap

    http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap.aspx

    You can make one of the size you want, and then copy pixels over to it.

    0 讨论(0)
  • 2021-01-01 00:05
    Bitmap      img   = (Bitmap) Image.FromStream(memStream);
    BitmapImage bmImg = new BitmapImage();
    
    using (MemoryStream memStream2 = new MemoryStream()) 
    {
       img.Save(memStream2, System.Drawing.Imaging.ImageFormat.Png);
       ms.Position = 0;
    
       bmImg.BeginInit();
       bmImg.CacheOption = BitmapCacheOption.OnLoad;
       bmImg.UriSource = null;
       bmImg.StreamSource = memStream2;
       bmImg.EndInit();
    }
    
    0 讨论(0)
  • 2021-01-01 00:09

    From this post

    public BitmapImage ImageFromBuffer(Byte[] bytes)
    {
        MemoryStream stream = new MemoryStream(bytes);
        BitmapImage image = new BitmapImage();
        image.BeginInit();
        image.StreamSource = stream;
        image.EndInit();
        return image;
    }
    
    0 讨论(0)
提交回复
热议问题