如何将 Byte[] 转换为 BitmapImage

发布于 2024-12-27 14:37:26 字数 552 浏览 2 评论 0原文

我需要帮助,我有这个方法可以从 Byte[]

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(); 获取 BitmapImage 抛出错误“我们发现没有适合完成此操作的成像组件。”

I need help, I have this method to get a BitmapImage from a Byte[]

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(); throws an error "We found no imaging component suitable to complete this operation."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

写下不归期 2025-01-03 14:37:26

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

Set Image.Source to a byte array property in XAML.

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

If you really want you can do this in code-behind:

public void DecodePhoto(byte[] byteVal)
{
  BitmapImage myBitmapImage = new BitmapImage();
  myBitmapImage.BeginInit();
  myBitmapImage.StreamSource = new MemoryStream(byteVal);
  myBitmapImage.DecodePixelWidth = 200;
  myBitmapImage.EndInit();
  MyImage.Source = myBitmapImage;
}
最后的乘客 2025-01-03 14:37:26

您应该向我们提供有关您的图片的更多信息。
我可以假设系统不支持它,然后我建议您使用外部工具,例如 imageMagik< /a> 或任何其他特定于您的图像的转换器。

You should provide us with more info on your image.
I can assume it's unsupported by the system, which I would then advice you to use an external tool, such as imageMagik or any other converter specific to your image.

神经暖 2025-01-03 14:37:26

我做了类似的东西,但它不是用 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){...}
  }

I've made something similar, but it's not with BitmapImage, hopes it can help...

First, I get the image from a path, so I get a BMP and cast into a 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;
  }

I call this in my code like this:

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

And when I want to cast the byte[] into System.Windows.Controls.Image I do this:

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){...}
  }
双手揣兜 2025-01-03 14:37:26

这也可能有帮助:

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

This might also help:

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文