如何转换类型Image到类型BitmapImage?
有谁知道如何从图像创建BitmapImage? 这是我正在使用的代码:
MemoryStream memStream = new MemoryStream(bytes); Image img = Image.FromStream(memStream);
现在我可以在内存中访问此图像。 问题是我需要将其转换为类型BitmapImage才能在我的解决方案中使用它。
注意:我需要将其转换为类型BitmapImage而不是类型Bitmap 。 我无法弄明白,因为类型BitmapImage为它的构造函数接受了一个Uri。
从这篇文章
public BitmapImage ImageFromBuffer(Byte[] bytes) { MemoryStream stream = new MemoryStream(bytes); BitmapImage image = new BitmapImage(); image.BeginInit(); image.StreamSource = stream; image.EndInit(); return image; }
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(); }
您确定需要BitmapImage,而不是BitmapSource吗? BitmapImage是BitmapSource的子类,专门用于在XAML(来自URI)中创建源。 还有许多其他BitmapSource选择 – 其他选择之一可能更好。
例如,WriteableBitmap
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap.aspx
你可以制作一个你想要的尺寸,然后将像素复制到它。