簡體   English   中英

如何將 Byte[] 轉換為 BitmapImage

[英]How to convert Byte[] to BitmapImage

我需要幫助,我有這個方法可以從 Byte[] 獲取 BitmapImage

public BitmapSource ByteToBitmapSource(byte[] image)
{
    BitmapImage imageSource = new BitmapImage();

    using (MemoryStream stream = new MemoryStream(image))
    {
        stream.Seek(0, SeekOrigin.Begin);
        imageSource.BeginInit();
        imageSource.StreamSource = stream;
        imageSource.CacheOption = BitmapCacheOption.OnLoad;
        imageSource.EndInit();
    }

    return imageSource;
}

imageSource.EndInit(); 拋出錯誤“我們發現沒有適合完成此操作的成像組件。”

Image.Source設置為 XAML 中的字節數組屬性。

<Image x:Name="MyImage" Source="{Binding Path=MyByteArrayProperty}" />

如果你真的想要,你可以在代碼隱藏中做到這一點:

public void DecodePhoto(byte[] byteVal)
{
  BitmapImage myBitmapImage = new BitmapImage();
  myBitmapImage.BeginInit();
  myBitmapImage.StreamSource = new MemoryStream(byteVal);
  myBitmapImage.DecodePixelWidth = 200;
  myBitmapImage.EndInit();
  MyImage.Source = myBitmapImage;
}

這也可能有幫助:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Drawing;
using System.Runtime.InteropServices;
using System.IO;
using System.ComponentModel;


public class MakeBitmapSource
{
    [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")]
    public static extern void CopyMemory(IntPtr Destination, IntPtr Source, uint Length);



    public static BitmapSource FromNativePointer(IntPtr pData, int w, int h, int ch)
    {
        PixelFormat format = PixelFormats.Default;

        if (ch == 1) format = PixelFormats.Gray8; //grey scale image 0-255
        if (ch == 3) format = PixelFormats.Bgr24; //RGB
        if (ch == 4) format = PixelFormats.Bgr32; //RGB + alpha


        WriteableBitmap wbm = new WriteableBitmap(w, h, 96, 96, format, null);
        CopyMemory(wbm.BackBuffer, pData, (uint)(w * h * ch));

        wbm.Lock();
        wbm.AddDirtyRect(new Int32Rect(0, 0, wbm.PixelWidth, wbm.PixelHeight));
        wbm.Unlock();

        return wbm;
    }

    public static BitmapSource FromArray(byte[] data, int w, int h, int ch)
    {
        PixelFormat format = PixelFormats.Default;

        if (ch == 1) format = PixelFormats.Gray8; //grey scale image 0-255
        if (ch == 3) format = PixelFormats.Bgr24; //RGB
        if (ch == 4) format = PixelFormats.Bgr32; //RGB + alpha


        WriteableBitmap wbm = new WriteableBitmap(w, h, 96, 96, format, null);
        wbm.WritePixels(new Int32Rect(0, 0, w, h), data, ch * w, 0);

        return wbm;
    }
}

您應該向我們提供有關您的圖像的更多信息。
我可以假設系統不支持它,然后我會建議您使用外部工具,例如imageMagik或任何其他特定於您的圖像的轉換器。

我做了類似的東西,但它不是用 BitmapImage,希望它可以幫助...

首先,我從路徑獲取圖像,因此我獲取 BMP 並轉換為字節 []:

private byte[] LoadImage(string szPathname)
  {
     try
     {
        Bitmap image = new Bitmap(szPathname, true);

        MemoryStream ms = new MemoryStream();
        image.Save(ms, ImageFormat.Bmp);
        return ms.ToArray();
     }catch (Exception){...}

     return null;
  }

我在我的代碼中這樣稱呼它:

byte[] bitmapData1 = LoadImage(@"C:\Users\toto\Desktop\test1.bmp");

當我想將 byte[] 轉換為 System.Windows.Controls.Image 時,我會這樣做:

protected virtual void OnByteArrayChanged(DependencyPropertyChangedEventArgs e)
  {
     try
     {
        // PropertyChanged method
        BitmapImage bmpi = new BitmapImage();
        bmpi.BeginInit();
        bmpi.StreamSource = new MemoryStream(ByteArray);
        bmpi.EndInit();

        System.Windows.Controls.Image image1 = (get my image in my xaml);
        image1.Source = bmpi;
     }catch (Exception){...}
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM