【问题标题】:Convert byte array to image in wpf在wpf中将字节数组转换为图像
【发布时间】:2012-03-05 09:19:13
【问题描述】:

我用过

private BitmapImage byteArrayToImage(byte[] byteArrayIn)
{
    try
    {               
        MemoryStream stream = new MemoryStream();
        stream.Write(byteArrayIn, 0, byteArrayIn.Length);
        stream.Position = 0;
        System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
        BitmapImage returnImage = new BitmapImage();
        returnImage.BeginInit();
        MemoryStream ms = new MemoryStream();
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
        ms.Seek(0, SeekOrigin.Begin);
        returnImage.StreamSource = ms;
        returnImage.EndInit();

        return returnImage;
    }
    catch (Exception ex)
    {
        throw ex;
    }
    return null;
}

我的应用程序中的这个方法将字节数组转换为图像。但它抛出“参数无效”异常..为什么会发生..?有什么替代方法吗??

【问题讨论】:

  • 哪一行抛出异常?
  • System.Drawing.Image img = System.Drawing.Image.FromStream(stream);此代码引发上述异常...
  • @BijoyKJose 我知道这是很久以前的事了,但是您找到解决问题的方法了吗"No imaging component suitable to complete this operation was found"。我现在遇到了同样的问题,我找不到任何解决方案。

标签: c# wpf


【解决方案1】:

您好,这应该可以工作:

    private static BitmapImage LoadImage(byte[] imageData)
    {
        if (imageData == null || imageData.Length == 0) return null;
        var image = new BitmapImage();
        using (var mem = new MemoryStream(imageData))
        {
            mem.Position = 0;
            image.BeginInit();
            image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
            image.CacheOption = BitmapCacheOption.OnLoad;
            image.UriSource = null;
            image.StreamSource = mem;
            image.EndInit();
        }
        image.Freeze();
        return image;
    }

【讨论】:

  • 我修改了我自己的代码 sn-p 并且我也使用了你的代码。但是在执行 image.EndInit(); 后两者都抛出异常“找不到适合完成此操作的成像组件”;有什么解决办法吗?
  • 是你的 imageData 集吗?如果首先将字节保存到流中或将图像文件的字节加载到内存中,则此方法有效 - 请参见此处:msdn.microsoft.com/de-de/library/…
  • 谢谢卡斯滕,你节省了我的时间 :)
【解决方案2】:

如果你有这样的数组:

byte[] byteArrayIn = new byte[] {255, 128, 0, 200};

你想要这样的东西:

用途:

BitmapSource bitmapSource = BitmapSource.Create(2, 2, 300, 300,PixelFormats.Indexed8,    BitmapPalettes.Gray256, byteArrayIn, 2);

Image.Source = bitmapSource;

在 xaml 中:

<Image RenderOptions.BitmapScalingMode="NearestNeighbor" RenderOptions.EdgeMode="Aliased" x:Name="Image"></Image>   

【讨论】:

  • 您的回答拯救了我的一天!很容易理解的本质。在 Pixelformats 和 BitmapPalettes 玩了 2 个小时后,发现它真的很简单很有趣。
【解决方案3】:
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnBrowse_Click(object sender, RoutedEventArgs e)
    {
        var of = new OpenFileDialog();
        of.Filter = "Image files (*.png;*.jpeg)|*.png;*.jpeg|All files (*.*)|*.*";
        var res = of.ShowDialog();
        if (res.HasValue)
        {
            imgPreview.Source = new BitmapImage(new Uri(of.FileName));

            var t = Utils.ConvertBitmapSourceToByteArray(imgPreview.Source as BitmapSource);
            var d = Utils.ConvertBitmapSourceToByteArray(new BitmapImage(new Uri(of.FileName)));
            var s = Utils.ConvertBitmapSourceToByteArray(imgPreview.Source);
            var enc = Utils.ConvertBitmapSourceToByteArray(new PngBitmapEncoder(), imgPreview.Source);
            //imgPreview2.Source = Utils.ConvertByteArrayToBitmapImage(enc);
            imgPreview2.Source = Utils.ConvertByteArrayToBitmapImage2(enc);
            //var i = 0;


        }
        else
        {
            MessageBox.Show("Select a currect file...");

        }
    }

}

/util.cs/

public class Utils
{
    public static byte[] ConvertBitmapSourceToByteArray(BitmapEncoder encoder, ImageSource imageSource)
    {
        byte[] bytes = null;
        var bitmapSource = imageSource as BitmapSource;

        if (bitmapSource != null)
        {
            encoder.Frames.Add(BitmapFrame.Create(bitmapSource));

            using (var stream = new MemoryStream())
            {
                encoder.Save(stream);
                bytes = stream.ToArray();
            }
        }

        return bytes;
    }

    public static byte[] ConvertBitmapSourceToByteArray(BitmapSource image)
    {
        byte[] data;
        BitmapEncoder encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(image));
        using (MemoryStream ms = new MemoryStream())
        {
            encoder.Save(ms);
            data = ms.ToArray();
        }
        return data;
    }
    public static byte[] ConvertBitmapSourceToByteArray(ImageSource imageSource)
    {
        var image = imageSource as BitmapSource;
        byte[] data;
        BitmapEncoder encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(image));
        using (MemoryStream ms = new MemoryStream())
        {
            encoder.Save(ms);
            data = ms.ToArray();
        }
        return data;
    }
    public static byte[] ConvertBitmapSourceToByteArray(Uri uri)
    {
        var image = new BitmapImage(uri);
        byte[] data;
        BitmapEncoder encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(image));
        using (MemoryStream ms = new MemoryStream())
        {
            encoder.Save(ms);
            data = ms.ToArray();
        }
        return data;
    }
    public static byte[] ConvertBitmapSourceToByteArray(string filepath)
    {
        var image = new BitmapImage(new Uri(filepath));
        byte[] data;
        BitmapEncoder encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(image));
        using (MemoryStream ms = new MemoryStream())
        {
            encoder.Save(ms);
            data = ms.ToArray();
        }
        return data;
    }

    public static BitmapImage ConvertByteArrayToBitmapImage(Byte[] bytes)
    {
        var stream = new MemoryStream(bytes);
        stream.Seek(0, SeekOrigin.Begin);
        var image = new BitmapImage();
        image.BeginInit();
        image.StreamSource = stream;
        image.EndInit();
        return image;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-16
    • 2013-05-19
    • 2016-03-01
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多