Circumventing GDI+ default image compression. RRS feed

  • Question

  • User671404075 posted
    I would like to grab an existing animated gif file and output it to the browser. I DON'T want GDI+ to mess with the compression, color-depth, dithering, etc. I've already created the image the way I want it. What else do I need to add to the following code, if anything, to ensure that my original image is not tampered with: Dim newBitmap As New Bitmap(Server.MapPath("photo03.gif")) Response.ContentType = "Image/Jpeg" newBitmap.Save(Response.OutputStream, ImageFormat.Jpeg) This would also be handy for avoiding the terrible compression that GDI+ adds to jpeg images. Ntoe: I'm not POSITIVE that GDI+ is, in fact, compressing my image, but I know that a dynamically generated image has less than desireable compression applied (especially .jpg images).
    Saturday, April 3, 2004 1:25 PM

All replies

  • User671404075 posted
    Correction. My code reads: Dim newBitmap As New Bitmap(Server.MapPath("photo03.gif")) Response.ContentType = "Image/Gif" newBitmap.Save(Response.OutputStream, ImageFormat.Gif) My question remains the same. Thanks.
    Saturday, April 3, 2004 1:52 PM
  • User-1372641848 posted
    ::I would like to grab an existing animated gif file and output it to the browser Then why you need to create bitmap object? and save to OutputStream.. Response.WriteFile(Server.MapPath("photo03.gif")) should do trick for you ::What else do I need to add to the following code, if anything, to ensure that my original ::image is not tampered with: gif and jpeg are totally different format if you need to convert you image to jpeg format then you will need to choose correct compression to improve quality of image.(there are lot of examples on web for that.)
    Saturday, April 3, 2004 1:57 PM
  • User671404075 posted
    ::Then why [do] you need to create bitmap object? Figure THAT out and you'll be a wealthy man! ::gif and jpeg are totally different format[s]... That's a revelation! Now, can somebody read my post and offer a serious answer. I don't need to CONVERT from gif to jpeg. I'm asking if GDI+ will change (reduce, etc.) my color pallet (for gifs) or further compress my jpegs. Do I need to do anything special in code to PREVENT GDI+ from altering my image. Or is my image left alone by default. To clarify even further: The reason I'm asking is because I noticed that when CREATING a bitmap from scratch and outputing it to the browser as a Jpeg, the quality looks about as bad as using level 4 compression in Photoshop or 50% in Fireworks. Either of these compressions is unacceptable. So, again, I'm asking if this kind of thing happens to images that are ALREADY created (whether they be gif or Jpeg).
    Saturday, April 3, 2004 6:19 PM
  • User-1372641848 posted
    Looks like you feel offended.. I totally misunderstood your question. my last answer to your question was based on your first question(before you reformatted). which deed not explained your problem correctly(at least compared to your last explanation) Any way this is what I have experienced so far with jpeg images. 1) open and save without manipulating image. Bitmap img = new Bitmap(path); img.Save(path2,ImageFormat.Jpeg); no change in file 2) open and save with image manipulation. Bitmap img = new Bitmap(path); Graphics g = Graphics.FromImage(img); g.DrawString("Png Image",new Font("Verdana",10),Brushes.Yellow,10,10); img.Save(path2,ImageFormat.Jpeg); gdi+ messes up with compression and file size changes drastically. I tried playing with Image.Save(String filename , ImageCodecInfo encoder , EncoderParameters encoderParams ) but with out any success I first times noticed this thing when I was trying to manipulate Exif information on jpg file even though I was not drawing any thing on image itself. It looked that image was recompressing after change in propertyItems. I would appreciate if any one can put more light on subject.
    Sunday, April 4, 2004 3:46 PM
  • User671404075 posted
    Thanks, Jigar, The first of your two examples provides the answer I needed. I'm with Jigar. It would be helpful in the future to know If anyone knows how to manipulate the compression settings in Jigar's second example.
    Monday, April 5, 2004 4:47 PM
  • User-865881720 posted
    I to have been surprised with the poor quality of JPEG compression. I read somewhere that the default quality is 70 (within a possible range of 0 to 100) with better quality (less compression) the higher the setting. Apparently there is little correlation among graphics programs as to what numeric settings of JPEG quality or compress really mean. In one program I frequently use (CompuPic by PhotoDex) a setting of 50 is quite good and settings of 70 result in very large file sizes. Using a quality setting of 50 in the GDI+ Image.Save method results in terrible quality. Here is the general procedure for setting JPEG quality (in this example at 80): 'Setup a JPEG codec with desired quality Dim Codec, JPEGCodec As System.Drawing.Imaging.ImageCodecInfo Dim EncParams As System.Drawing.Imaging.EncoderParameters Dim QualitySetting As Long = 80L For Each Codec In System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders If Codec.MimeType = "image/jpeg" Then JPEGCodec = Codec Exit For End If Next 'Supposedly this cannot fail as image/jpg is built-in to GDI+ encoders EncParams = New System.Drawing.Imaging.EncoderParameters(1) EncParams.Param(0) = New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, QualitySetting) 'Save the full-size image as a .jpg with specified quality MyImage.Save(ImageFileName, JPEGCodec, EncParams)
    Monday, April 5, 2004 9:44 PM