[英]How to convert from type Image to type BitmapImage?
有誰知道如何從圖像創建位圖圖像? 這是我正在使用的代碼:
MemoryStream memStream = new MemoryStream(bytes);
Image img = Image.FromStream(memStream);
現在我可以訪問內存中的這張圖片。 問題是我需要將其轉換為 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);
memStream2.Position = 0;
bmImg.BeginInit();
bmImg.CacheOption = BitmapCacheOption.OnLoad;
bmImg.UriSource = null;
bmImg.StreamSource = memStream2;
bmImg.EndInit();
}
從這個帖子
public BitmapImage ImageFromBuffer(Byte[] bytes)
{
MemoryStream stream = new MemoryStream(bytes);
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = stream;
image.EndInit();
return image;
}
你確定你需要 BitmapImage,而不是 BitmapSource? BitmapImage 是 BitmapSource 的子類,專門用於在 XAML(來自 URI)中制作源。 還有許多其他 BitmapSource 選擇——其他選擇之一可能更好。
例如,可寫位圖
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap.aspx
您可以制作您想要的尺寸之一,然后將像素復制到它上面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.