.net drawing to bitmap using FillRectangle fails when Magenta
var bmp = new Bitmap(16, 16);
var gBmp = System.Drawing.Graphics.FromImage(bmp );
Color col = Color.FromArgb(pdmsCol.Red, pdmsCol.Green, pdmsCol.Blue);
gBmp.FillRectangle(new SolidBrush(col), new Rectangle(0, 0, 16, 16));
mColourPopupContainer.Image = bmp;
Using the above code draws a rectangle into my control for given color. This works fine unless the color is Magenta in which case it seems to be drawn transparently. I guess this is something to do with bitmaps treating Magenta as transparent. How to I turn off this behaviour?
m开发者_运维问答ColourPopupContainer is a UserControl (custom color picker).
You need to set the Form.TransparencyKey
property to a color other than Magenta. This property:
Gets or sets the color that will represent transparent areas of the form.
By default it's magenta, hence your issue. If you set it to some other color that shouldn't show up anywhere else on your form, the problem should go away.
My current solution is not very nice: Detect when magenta is used and change it to not magenta.
var col = Color.FromArgb(r, g, b);
if (r==255 && g==0 && b==255 ) col = Color.FromArgb(r-1, g, b); // Don't use Megenta as it can be treated as transparent
精彩评论