如何将Byte[]转换为BitmapImage

17

我需要帮助,我有一个从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(); 抛出错误 "We found no imaging component suitable to complete this operation."


2
你尝试加载的图片是什么格式?你确定它被支持吗? - Shimmy Weitzhandler
4个回答

27

在XAML中将Image.Source设置为一个字节数组属性。

<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;
}

当Image.Source是ImageSource类型时,您该如何将其设置为byte[]? - TimothyP

0

您应该提供更多关于您的图像的信息。
我可以假设它不受系统支持,那么我建议您使用外部工具,例如imageMagik或任何适用于您的图像的转换器。


0

我做过类似的东西,但不是用BitmapImage,希望可以帮到你...

首先,我从路径中获取图像,所以我得到一个BMP并将其转换为byte[]:

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){...}
  }

0

这可能也会有所帮助:

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;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接