我要将一个刻度图保存到一个字节[]中,以保存DB。我很确定数据被准确保存和检索,因此这不是问题。

在我的字节[]到bitmapimage转换上,我不断获得“ system.notsupportedException:没有适合完成此操作的成像组件”。

谁能在这里看到我的两个功能我在做错什么?

  private Byte[] convertBitmapImageToBytestream(BitmapImage bi)
  {
     int height = bi.PixelHeight;
     int width = bi.PixelWidth;
     int stride = width * ((bi.Format.BitsPerPixel + 7) / 8);

     Byte[] bits = new Byte[height * stride];
     bi.CopyPixels(bits, stride, 0);

     return bits;
  }

  public BitmapImage convertByteToBitmapImage(Byte[] bytes)
  {
     MemoryStream stream = new MemoryStream(bytes);
     stream.Position = 0;
     BitmapImage bi = new BitmapImage();
     bi.BeginInit();
     bi.StreamSource = stream;
     bi.EndInit();
     return bi;
  }
有帮助吗?

解决方案 3

事实证明,位模型复型不正确。我采用位图像的输出,并将其转换为JPG的可用物品。

public static Byte[] convertBitmapImageToBytestream(BitmapImage bi)
  {
     MemoryStream memStream = new MemoryStream();
     JpegBitmapEncoder encoder = new JpegBitmapEncoder();
     encoder.Frames.Add(BitmapFrame.Create(bi));
     encoder.Save(memStream);
     byte[] bytestream = memStream.GetBuffer();
     return bytestream;
  }

其他提示

这个Stackoverflow问题有帮助吗?

Byte []到Silverlight中的bitmapimage

编辑:

尝试一下,不确定它是否有效:

public BitmapImage convertByteToBitmapImage(Byte[] bytes)
{
    MemoryStream stream = new MemoryStream(bytes);
    stream.Position = 0;
    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.CacheOption = BitmapCacheOption.OnLoad;
    bi.DecodePixelWidth = ??; // Width of the image
    bi.StreamSource = stream;
    bi.EndInit();
    return bi;
}

更新2:

我发现这些:

在运行时加载一个字节[]到图像中

来自byte []的bitmapimage在非uithread上

除此之外,我不知道。

您怎么知道您正在创建的字节[]格式是BI在流中的期望?为什么不使用bitmapimage.streamsource创建保存的字节[]?然后,您知道格式将兼容。

http://www.codeproject.com/kb/vb/bmpimage2bytearray.aspx

http://social.msdn.microsoft.com/forums/en-us/wpf/thread/8327dd31-2b1-4daa-4daa-a81c-aff60b63fee6/

我没有尝试任何此代码,但是您可以

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top