Question

I am trying to save an image to a MemoryStream to get a byte array.

 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

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). The latter is an image I receive through an SDK, which presumably take it out of a database stored in the network. The images are both System.Drawing.Image

What could be the issue here ?

No correct solution

OTHER TIPS

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. The images are both 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(... .

In which form you will receive image as stream object or System.Drawing.Image object?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top