Question

I'm trying to dynamically generate a gif image of a specified size and color in an HttpHandler. For some reason, the image is being generated with a dithered color pattern rather than the solid color I expected the SolidBrush to create. The following code is the simplest I could find that exposes the problem.

private void GenerateSquareImage(String filename, String color, Int32 width)
{
    Bitmap img = new Bitmap(width, width);
    Graphics g = Graphics.FromImage(img);
    SolidBrush brush = new SolidBrush(ColorTranslator.FromHtml("#" + color));

    g.FillRectangle(brush, 0, 0, width, width);

    img.Save(filename, System.Drawing.Imaging.ImageFormat.Gif);
    img.Dispose();

    brush.Dispose();
    g.Dispose();
}

Here is a sample of the results I'm getting when passing "415976" in the color parameter:

imageshack http://img148.imageshack.us/img148/8936/colorcb5.png

(The block on the left is an HTML SPAN with background-color set to #415976. The block on the right is my graphic, where you can see the dithered colors.)

The result is the same if I use this code:

g.Clear(ColorTranslator.FromHtml("#" + color));

in place of the g.FillRectangle call in the method above.

UPDATE: By default, GDI+ saves GIFs with the web safe palette if any programmatic changes are made to the Bitmap. This is what was causing my non safe palette color to be dithered. I was able to modify the code referenced in Marco's post below (http://msdn.microsoft.com/en-us/library/aa479306.aspx) to modify the palette on my freshly created image so that my color was respected.

Was it helpful?

Solution

GIF files are limited to 256 colors as far as I know, so it resorts to dithering. PNG is probably the format you need.

Or you can look here for a solution using a complex quantizer to build a custom palette (I don't take responsibility for the code in the link, never tried myself).

This other link from Microsoft has an in-depth analysis of the problem.

Hope this helps.

OTHER TIPS

IIRC GIF's are limited to a specific pallete on .NET. There are workarounds, but they were above my understanding.

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