.NET Framework - 8 bit indexed bitmap object saves to jpg as 24 bit...

Asked By James Maeding
22-Jan-07 08:11 PM
I've been studying all the articles I can find on GDI+, and putting together a function to convert image colors and
types.
I am finding that I can quantize an image to 8 bit colors, and then save to png format perfectly with:
bitmap.save(filename,  ImageFormat.png)
When I do the same thing to jpg, I get a 24 bit color jpg every time.
I know jpg supports an indexed 8 bit palette, and can make them all day in other progs like corel photo-paint.

I can watch the bitmap object to inspect it just before the save, its exactly the same every time.
The only difference is I am doing bitmap.save(filename, ImageFormat.Jpeg) instead of bitmap.save(filename,
ImageFormat.png)

I tried going the bitmap.save(filename, ImageCodecInfo, EncoderParameters) in case I needed to set the quality of the
jpg to 100 or something.  didn't work.

Any suggestions?  I must be missing something simple.  I thought if I start with the exact same bitmap object with same
properties for both cases, the save method would behave but its not.
thanks
ImageCodecInfo
(1)
ImageFormat
(1)
EncoderParameters
(1)
Paint
(1)
  Nik Coughlin replied...
22-Jan-07 08:47 PM
Where you're going wrong is, despite what you "know", jpeg doesn't support
indexed palettes at all.  It's a lossy compression format intended for
storing photograph-type images.  It is fundamentally unsuited to indexed
images.
  Michael C replied...
22-Jan-07 09:59 PM
Paint shop pro forces the image to 24 bit. Maybe this is a feature only of
corel and not generally supported?

Michael
  James Maeding replied...
22-Jan-07 11:00 PM
funny how we can be our own worst enemy.  I am actually hoping you are right because that would answer my question.
The reason I think jpg's can support indexed is I can open one in corel photo paint, and convert color to 8 bit
paletted.  I can load that into a bitmap object and the palete is 256 colors, indexed.
I was sold on that but maybe that is a bit special (no pun..) somehow.
I agree its a lossy format, and have no opinion on if its suited to indexed colors.
I just know the paletted ones are a lot smaller than the 24 bit.
I'll be going with png's, but I wanted to be able to go to any format with my tool.
thx
  James Maeding replied...
22-Jan-07 11:02 PM
it seems that might be the case - and that is fine with me.  I do not like jpg a lot as most people do not realize its
lossier with every save.  thx for the reply
  Nik Coughlin replied...
22-Jan-07 11:19 PM
Interesting.  What happens if you open a jpeg, convert to 8 bit indexed,
save it as jpeg, close the file, then re-open it?
  James Maeding replied...
22-Jan-07 11:39 PM
wow, I wonder what else I have been missing :)
you are dead on correct, it flips back to 24 bit color in corel pp.
I guess I so rarely use jpg's (I use tif's) that I didn't notice it.  I could have sworn it worked, you should have made
a bet with me <g>.
Its like fighting with my wife now, the more I say the dumber I look....

thanks for the schooling.
  Nik Coughlin replied...
22-Jan-07 11:48 PM
Betcha $50 I am right :P

Oops, too late
Create New Account
help
SetDataObject (1) System.Windows.Forms.IDataObject (1) System.Windows.Forms.DataFormats (1) ControlStyles.SupportsTransparentBackColor (1) ImageCodecInfo.GetImageEncoders (1) System.Drawing.Imaging.Metafile (1) IImageList.GetItemFlags (1) Windows XP (1) This is like PNG) is to save it physically to disk, and specify the format using the ImageFormat enumeration. . how would I copy this to the clipboard in PNG format? TIA Chadwick MemoryStream ms = new MemoryStream(); bmp.Save(ms, ImageFormat.Png); IDataObject dataObject = new DataObject(); dataObject.SetData("PNG", false, ms); System.Windows.Forms.Clipboard.SetDataObject as with the clipboard and the DataObject? Thanks MemoryStream ms = new MemoryStream(); bmp.Save(ms, ImageFormat.Png); IDataObject dataObject = new DataObject(); dataObject.SetData("PNG", false, ms); this.DoDragDrop(dataObject, DragDropEffects.All work as I think it should do. When I copy my bitmap onto the clipboard, Paint.NET tells me "The clipboard doesn't contain an image". Is this a bug in Paint.NET ('normal' paint does nothing when the image is pasted in . . no error message and no image) or code below? The file saved to disk in the example, mycontrol.png, looks fine in Paint.NET with the checker-board transparency color present. Any thoughts gratefully received! namespace WindowsFormsApplication1 { public
ImageCodecInfo and array's references .NET Framework Hi, In order to save image file with some additional encoder parameters, you need to use System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders() array to specify with image codec you want to use. I searched MSDN or with no enumarating array items: http: / / msdn2.microsoft.com / en-us / library / system.drawing.imaging.imagecodecinfo.getimageencoders.aspx Thanks. . . VB.NET Discussions ImageCodecInfo.GetImageDecoders (1) ImageCodecInfo (1) Imaging.EncoderParameters (1) EncoderParameters (1) GetImageDecoders (1) GetImageEncoders (1) ImageFormat (1) JgpEncoder.FilenameExtension.Split (1) I don't think there is a guaranteed order of
adding a Paint event to a interface .NET Framework Hi, I have a interface that I use for form so I can pass the form to another object. How do I add the Paint event to the interface and subsequently handle the paint event in my other object.? I am not sure of the syntax required to do TabPageControl (1) PaintEventArgs (1) PaintOnForm (1) On Aug 28, 10:41 am, "Rotsey" Declare the Paint event in the interface: public event EventHandler Paint; (or whatever). Then implement the event in your form in the normal way (either with line and then get errors saying form does not implement paiint so I added void Paint(object sender, PaintEventArgs e) { } Then I get error "paint hides inherited member System.Windows.Form.Paint ??? I thought that after adding your line that the forms as they have a Paint
imgInput bmapTemp.SetResolution(intOutputResolution, intOutputResolution) imgOutput = New Bitmap(bmapTemp) imgOutput.Save(strmOutput, System.Drawing.Imaging.ImageFormat.Jpeg) ' Return the output image in a byte array ChangeImageResolution = strmOutput.ToArray End Function Private image imgOutput = New Bitmap(imgInput, sizResize.Width, sizResize.Height) imgOutput.Save(strmOutput, System.Drawing.Imaging.ImageFormat.Jpeg) ' Return the output image in a byte array ResizeImage = strmOutput.ToArray End Function VB.NET Discussions System.Drawing.Image.FromStream (1) ImageCodecInfo.GetImageEncoders (1) System.IO.MemoryStream (1) FileMode.OpenOrCreate (1) System.Drawing.Image (1 ImageCodecInfo (1) ImageToJpegToBytes (1) EncoderParameters (1) I guess you know that the line above only copies the reference, not the is a byte array containing a 96 dpi jpeg. imgOutput.Save(strmOutput, System.Drawing.Imaging.ImageFormat.Jpeg) ' Return the output image in a byte array ResizeImage = strmOutput.ToArray Do you have Codecs into bm bm.SetResolution(72, 72) bm.Save("C: \ test1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg) or bm.SetResolution(100, 100) bm.Save("C: \ test2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg) Using Codecs allows control over image quality, etc. also. An example of Codec usage