简体   繁体   中英

RadioButtonRenderer with Transparent Background

I have a situation where I need to render a radio button to an System.Drawing.Image.

System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(20,16);
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{                                    
    RadioButtonRenderer.DrawRadioButton(g, new Point(2, 2), RadioButtonState.CheckedPressed);
}
return System.Drawing.Image.FromHbitmap(bitmap.GetHbitmap());

This works except that it's drawing the default control-grey as the background. How can I set this the background color to Transparent?

Not sure how your button looks exactly, but using the Bitmap.MakeTransparent Method with the grey color should work.

If it is not to your liking, you can try more complex color transformation using the ColorMatrix Class :

ColorMatrix matrix = new ColorMatrix();

// This will change the image's opacity.
matrix.Matrix33 = 0.5f;

ImageAttributes imgAttrs = new ImageAttributes();
imgAttrs.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

g.DrawImage(img, new Rectangle(0, 0, 50, 50), 0, 0, 50, 50, GraphicsUnit.Pixel, imgAttrs);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM