简体   繁体   English

无法将图像保存到MemoryStream

[英]Can't save an Image to MemoryStream

I am trying to save an image to a MemoryStream to get a byte array. 我正在尝试将图像保存到MemoryStream以获得字节数组。

 using (MemoryStream ms = new MemoryStream()){
      // Convert Image to byte[]
      ms.Seek(0, SeekOrigin.Begin);
      img.Save(ms, ImageFormat.RawFormat);
      byte[] imageBytes = ms.ToArray();
      return imageByates;
  }

This piece of code works for an image with the following properties 这段代码适用于具有以下属性的图像

Flags 77840
FrameDimensionList System.Guid[]
Heigth 2048
HorizontalResolution 72
Palette System.Drawing.Imaging.ColorPalette
PhysicalDimension {Width=1322, Height=2048}
Pixelformat Format24bppRgb
PropertyIDList 0 20625
PropertyIDList 1 20624
PropertyIDList 2 34675
PropertyItems System.Drawing.Imaging.PropertyItem
PropertyItems 0 System.Drawing.Imaging.PropertyItem
PropertyItems 1 System.Drawing.Imaging.PropertyItem
PropertyItems 2 System.Drawing.Imaging.PropertyItem
RawFormat [ImageFormat: b96b3cae-0728-11d3-9d7b-0000f81ef32e]
RawFormat height 2048 width 1322
Tag
VerticalResolution 72
Width 1322

I get the infamous a Generic Error Occured in gdi+ for another image with following properties 我收到了臭名昭著的通用错误,该错误发生在gdi +中,是另一个具有以下属性的图像

Flags 73744
FrameDimensionList System.Guid[]
Heigth 336
HorizontalResolution 96
Palette System.Drawing.Imaging.ColorPalette
PhysicalDimension {Width=251, Height=336}
Pixelformat Format24bppRgb
RawFormat [ImageFormat: b96b3cae-0728-11d3-9d7b-0000f81ef32e]
RawFormat height 336 width 251
Tag
VerticalResolution 96
Width 251

The difference, is former is a jpeg image I loaded from filesystem (using Image.FromFile ). 区别在于,前者是我从文件系统(使用Image.FromFile )加载的jpeg图像。 The latter is an image I receive through an SDK, which presumably take it out of a database stored in the network. 后者是我通过SDK接收到的图像,大概是从网络中存储的数据库中取出的。 The images are both System.Drawing.Image 图像都是System.Drawing.Image

What could be the issue here ? 这可能是什么问题?

Need bit more clarity on last para from your question. 您的问题的最后一段需要更多的说明。

The latter is a Image I receive through an SDK, which presumably take it out of a database stored in the network. 后者是我通过SDK收到的图片,大概是从网络中存储的数据库中取出的。 The images are both System.Drawing.Image 图像都是System.Drawing.Image

If you are receiving Stream from referenced SDK then you just need to read the stream to get byte array no need to save with Image.Save(... . 如果您从引用的SDK接收Stream,则只需读取流即可获取字节数组,而无需使用Image.Save(...保存。

In which form you will receive image as stream object or System.Drawing.Image object? 您将以哪种形式将图像作为流对象或System.Drawing.Image对象接收?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM