【问题标题】:How to convert a byte-array to a Image in C#?如何在 C# 中将字节数组转换为图像?
【发布时间】:2015-02-03 08:26:06
【问题描述】:

我有一个存储 PNG 的 SQL Server 数据库。屏幕截图的值是十六进制(0x085A3B ...)。如何从“Screenshot”(我自己的数据类型)转换为“Image”或类似“BitmapImage”之类的东西?

首先,我截取如下截图:

private Screenshot LoadScreenshot()
{
    using (var context = new Context())
    {
        return context.Screenshots.FirstOrDefault();
    }
}

上面的方法返回一个字节数组,比如

byte[40864]

我无法执行以下操作,因为我得到了一个异常(我不知道是哪一个以及为什么):

public BitmapImage ImageFromBuffer(Byte[] bytes)
{
    MemoryStream stream = new MemoryStream(bytes);
    BitmapImage image = new BitmapImage();
    image.BeginInit();
    image.StreamSource = stream;
    image.EndInit(); //the compiler breaks here
    return image;
}

我正在使用 C# 和 WPF

谢谢

编辑:

这是我的例外:

System.Runtime.Serialization.SafeSerializationManager 未找到适合完成此操作的成像组件

如何解决:

我需要添加这行代码:

Byte[] screenshotBytes = screenshot.Screenshot; //.Screenshot is a byte [] (I dont knwo why it didnt work before)

还有@frebinfrancis 方法

【问题讨论】:

  • 我看到了他的帖子。我的问题是我遇到了异常
  • 那个问题也有例外。
  • 答案对我不起作用
  • 异常是运行时错误,而不是编译时问题。您可以发布您获得的异常的详细信息吗?另外,您是否尝试过将该字节数组保存到扩展名为 .png 的文件中 - 是否生成了正确的图像?

标签: c# .net wpf image


【解决方案1】:

您的代码看起来不错,并且您的代码没有问题,当我做同样的事情时,有些图像对我有用,但有些则不行。经过长时间的搜索,我找到了以下链接。

http://support.microsoft.com/kb/2771290

这是我的代码:

public BitmapImage ImageFromBuffer(Byte[] bytes)
        {
            if (bytes == null || bytes.Length == 0) return null;
            var image = new BitmapImage();
            using (var mem = new MemoryStream(bytes))
            {
                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;
        }

【讨论】:

    【解决方案2】:

    试试这个:

    public void SaveBitmap(string fileName, int width, int height, byte[] imageData)
    {
      var data = new byte[width * height * 4];
    
      int o = 0;
    
      for (var i = 0; i < width * height; i++)
      {
         var value = imageData[i];
    
         data[o++] = value;
         data[o++] = value;
         data[o++] = value;
         data[o++] = 0;
      }
    
      unsafe
      {
         fixed (byte* ptr = data)
         {
            using (Bitmap image = new Bitmap(width, height, width * 4,PixelFormat.Format32bppRgb, new IntPtr(ptr)))
            {
               image.Save(Path.ChangeExtension(fileName, ".bmp"));
            }
          }
       }
    }
    

    【讨论】:

    • 没有类位图
    • @NaMe System.Drawing.Bitmap
    • 仍然没有推荐人(我错过了一个大会吗?)
    • 谷歌搜索完整的类型名称通常会打开一个 MSDN 页面,该页面记录了该类型,包括它所在的程序集:msdn.microsoft.com/en-us/library/…
    • @NaMe,你还没有弄清楚Bitmap 的问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 2011-04-17
    • 2011-06-01
    • 2023-03-09
    相关资源
    最近更新 更多