views:

105

answers:

3

Hi guys

I'm developing a ASP.Net web handler that returns images making a in-memory System.Windows.Forms.Control and then exports the rendered control as a bitmap compressed to PNG, using the method DrawToBitmap(). The classes are fully working by now, except for a problem with the color assignment. By example, this is a Gauge generated by the web handler.

The gauge

The colors assigned to the inner parts of the gauge are red (#FF0000), yellow (#FFFF00) and green (#00FF00) but I only get a dully version of each color (#CB100F for red, #CCB70D for yellow and #04D50D for green).

The background is a bmp file containing the color gradient. The color loss happens whether the background is a gradient as the sample, a black canvas, a white canvas, a transparent canvas, even when there is not a background set.

  • With black background

With black background

  • With transparent background

With a transparent background

  • With a white background

With a white background

  • Without a background set

Without a background set

  • With pixel format in Format32bppArgb

With pixelformat Format32bppArgb

I've tried multiple bitmap color deeps, output formats, compression level, etc. but none of them seems to work. Any ideas?

This is a excerpt of the source code:

Bitmap bmp = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Image bgimage = (Image) HttpContext.GetGlobalResourceObject("GraphicResources", "GaugeBackgroundImage");
Canvas control_canvas = new Canvas(); //inherits from Control
....
//the routine that makes the gauge
....
control_canvas.DrawToBitmap(bmp, new Rectangle(0, 0, w, h));
+1  A: 

When you paint the colors, check to make sure the alpha channel is set correctly as well. If you have some alpha it will blend with that dark background and give you a darker color.

Nate Zaugg
the result is the same whether a background is present or not
Rodrigo
I've been trying to come up with a better answer. Maybe if you posted your code or sent it to me to "tinker" with?
Nate Zaugg
+1  A: 

Did you try this with only PNGs? I hit this problem with PNGs (or JPEGS?) in a browser long ago, but IIRC GIFs eliminated the problem:

NOTE that I'm not saying GIFs will solve your problem in the long wrong but it may tell you if the PNG write/read is the problem. Try it, try BMP, try something. Don't believe the PNG. You could try dumping the r g b values to a log file (or to disk as raw and importing into Photoshop and) see what the numbers are before writing out the bitmap. (Use LockBits; there are tutorials for code on getting bytes in and out of Bitmaps).

Jared Updike
Gamma correction won't affect any 0 or $ff values.
Mark Ransom
@Mark: Good point. Really these images look like someone loaded them in Photoshop and hit Ctrl+Y (CMYK mode) which makes things look like they came out of a printer, eliminating bright colors.
Jared Updike
A: 

I just realized that you have really bright yellow parts on that triangle pointer, so the problem is likely with the background image. Are you drawing those arcs with primitives or loading a bitmap? You could be having gamma problems, color space problems, etc. depending on the input format, assuming you are using an image for the red/yellow/green meter part. Try drawing those shapes in code if you are using an image. Whatever code makes the yellow triangle is getting the bright colors, so use the same image format or Brush or whatever makes the yellow triangle work.

Jared Updike
The triangle is filled with Color.Gold, but neither met the specified.
Rodrigo