In C#, .NET 2.0, Windows Forms, Visual Studio Express 2010, I'm saving an image made of the same color:
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
Brush brush = new SolidBrush(color);
graphics.FillRectangle(brush, 0, 0, width, height);
brush.Dispose();
}
bitmap.Save("test.png");
bitmap.Save("test.bmp");
If I'm using, for example
Color [A=153, R=193, G=204, B=17] or #C1CC11
after I'm saving the image and open it in an external viewer such as Paint.NET, IrfanView, XNView, etc. I am told that the color of the image is actually:
Color [A=153, R=193, G=203, B=16] or #C1CB10
so it's a similar color, but not the same!
I tried with both PNG and BMP saving.
When transparency (alpha) is involved, .NET saves a different color! When the alpha is 255 (no transparency), it saves the corrent color.
Thank you, Joe and Hans Passant for your comments.
Yes, as Joe said, the problem is on the line:
Here GDI+ modifies the color with a similar color, but not the exact one.
It seems that the solution is to write the color values directly in the pixels, using Bitmap.LockBits and Marshal.Copy:
Just wanted to add to this a little. From my experience, the last code snippet probably will throw a runtime error. This is due to the fact that the array of argb values in the bitmap is stored like this: [a1, b1, g1, r1, a2, b2, g2, r2, a3,...] etc., where a, r, g, b are int. You should run some test cases on your images to see where exactly the a,r,g,b values are in the array. Bitmaps don't necessarily guarantee this ordinality (yes, even in .Net).
To re-write it a little:
This would set a single pixel to blue with a transparency of 50% (or it should)... I'm still working on it myself...