How to get the "real" colors when exporting a GDI drawing to bmp

Posted by Rodrigo on Stack Overflow See other posts from Stack Overflow or by Rodrigo
Published on 2010-05-03T16:32:46Z Indexed on 2010/05/03 21:58 UTC
Read the original article Hit count: 348

Filed under:
|
|
|
|

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();
....
//the routine that makes the gauge
....
control_canvas.DrawToBitmap(bmp, new Rectangle(0, 0, w, h));

© Stack Overflow or respective owner

Related posts about c#

Related posts about gdi