Edw*_*ard 7 .net c# image image-processing
有谁知道如何从图像创建BitmapImage?这是我正在使用的代码:
MemoryStream memStream = new MemoryStream(bytes);
Image img = Image.FromStream(memStream);
Run Code Online (Sandbox Code Playgroud)
现在我可以在内存中访问此图像.问题是我需要将其转换为类型BitmapImage才能在我的解决方案中使用它.
注意:我需要将其转换为类型BitmapImage而不是类型Bitmap.我无法弄明白,因为类型BitmapImage为它的构造函数接受了一个Uri.
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();
}
Run Code Online (Sandbox Code Playgroud)
从这篇文章
public BitmapImage ImageFromBuffer(Byte[] bytes)
{
MemoryStream stream = new MemoryStream(bytes);
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = stream;
image.EndInit();
return image;
}
Run Code Online (Sandbox Code Playgroud)