fschultz / NetImageLibrary

Library to help with image handling such as resizing, cropping, applying filters (brightess, contrast, unsharpen mask, chroma key etc), watermarking, thumbnail creation, opening/saving files or streams and more.
http://kaliko.com/image-library/
Other
90 stars 27 forks source link

A Graphics object cannot be created from an image #4

Closed mrcode86 closed 8 years ago

mrcode86 commented 9 years ago

Hi.

I have this: private void CreatePngImage(Stream str) { using (var Temp = System.Drawing.Image.FromStream(str)) { using (var Image = new KalikoImage(Temp)) { }
} }

But on some image i get this: A Graphics object cannot be created from an image that has an indexed pixel format. on at System.Drawing.Graphics.FromImage(Image image) at Kaliko.ImageLibrary.KalikoImage..ctor(Image image) at MainImage.CreatePngImage(Stream str)

I have try this:

private void CreatePngImage(Stream str) { using (var Temp = System.Drawing.Image.FromStream(str)) { using (var NewBitmap = new Bitmap(Temp.Width, Temp.Height)) { using (var Gr = Graphics.FromImage(NewBitmap)) { Gr.DrawImage(Temp, 0, 0); }

using (var Image = new KalikoImage(NewBitmap)) { } } } }

But i se the same error.

What can i do to fix it?

fschultz commented 9 years ago

This can be fixed by sending the stream to your KalikoImage instance directly, like:

private void CreatePngImage(Stream str) {
  using (var Image = new KalikoImage(str)) {
    //..
  }
}

This will automatically convert any indexed pixel format.

Unfortunately the code that handles this transformation isn't applied in the constructor that takes a System.Drawing.Image as argument, but it is in the one taking a stream and the one taking a file path. I will fix this also for System.Drawing.Image in the upcoming release, but in the meantime KalikoImage(Stream stream) will do the trick.

Hope this helps!