My gifs are too noisy

I need some help. I wrote some code to pull some images from a database and write them on a web page. Nothing special, the code is working well, except for one very annoying thing.

The pictures in Gif format come out with some noise (see example), dithered if you prefer. I know how to fix the problem for the images I create myself using a Graphics object but I can't figure out how to solve this one.



The JPeg are fine, but I am stuck with the Gifs.

I am sure it has something to do with ContentType, because my data is in a Stream, so obviously has to know the mime type to render the right format.

I tried this, but no success (the code is longer than that, but I removed  the database part):

Dim bmp As Bitmap= New Bitmap(Processtoretrievemypicture)

HttpContext.Current.Response.ContentType = "image/gif"

bmp.Save(HttpContext.Current.Response.OutputStream, ImageFormat.Gif)

If only I could use the same method as I did before with a Graphics object like this:

Dim g As System.Drawing.Graphics

g.TextRenderingHint = TextRenderingHint.AntiAlias

This was the right solution but now I don't find a way to associate this with my Stream. I got this error message:

A Graphics object cannot be created from an image that has an indexed pixel format

By the way I found also a nice GDI+ Faq.

 

5 Comments

  • GIFs can only handle 256 color images. Use PNG if you want true color lossless images. Or you can handle the color reduction yourself and not dither colors but rather use closest color method.



    And TextRenderingHint is exactly what it says - it affects text rendering. Not color reduction.

  • Jerry the problem here is not the format but the way GDI render the Gif format.



    This image exist also as normal file and it's perfectly fine.



    I know TextrenderingHint is for text, but it's working also for ther graphics objects. I am looking for a solution to have my Gifs as they are.

  • If the image exists as a gif file already why don't you just dump the file? Why do you process it through GDI+? Just use HttpResponse.WriteFile, it'll be a lot faster then loading up the image into GDI+ objects only to dump it back.



    And if your images are not stored as regular files then you should use the code in Paul's link, that's exactly what you need.

  • I think Jerry is getting at the solution...



    Is there any reason you need to manipulate the image using GDI+ (add watermarks, copyright text, etc)?



    If not, do you still get these problems when you dump the image stream (from the database) to the Response stream directly?



    Load your image stream into a byte array, and write it out using Response.OutputStream.Write(). Or, wrap a BinaryReader on your image stream and a BinaryWriter on your Response.OutputStream, and do a bunch of ReadByte() WriteBytes(). I'm not sure which method is more efficient (dump to an array, or byte-by-byte).

  • I vote for dropping the Bitmap aspect and just dump the database stream direct to response.outputstream



    If you want to watermark the image you probably should do that on the input side, for performance reasons.

Comments have been disabled for this content.