Search code examples
c#bitmapgdisystem.drawingcolor-mapping

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


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));

Solution

  • Canvas was a class provided by a third party. We found a bug in their package, they released a new version, and the problem was finally solved. Thanks anyway for your time!