简体   繁体   English

具有透明背景的RadioButtonRenderer

[英]RadioButtonRenderer with Transparent Background

I have a situation where I need to render a radio button to an System.Drawing.Image. 我遇到一种情况,需要向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. 不确定按钮的外观如何,但是可以将Bitmap.MakeTransparent方法与灰色配合使用。

If it is not to your liking, you can try more complex color transformation using the ColorMatrix Class : 如果不喜欢,可以尝试使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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