c# - 具有透明背景的 RadioButtonRenderer

标签 c# winforms drawing radio-button

我有一种情况需要将单选按钮渲染到 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());

除将默认的控制灰色绘制为背景外,此方法有效。如何将此背景颜色设置为透明?

最佳答案

不确定您的按钮的外观,但使用 Bitmap.MakeTransparent Method灰色应该可以。

如果您不喜欢,可以使用 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);

关于c# - 具有透明背景的 RadioButtonRenderer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5266105/

相关文章:

c# - 计时器说它已启用,但从不执行

ios - 重构代码后使用 self.view

ios - iOS系统如何绘制 View 角形状?

c# - 如何在 C# String 和 C Char 指针之间进行转换

c# - 从 GridView 项目模板中删除多余的空格

c# - WebBrowser 控件禁用鼠标点击

c# - 如何获取 DataGridView 控件上滚动条的大小?

c# - MVC 删除后将数据库记录追加到表中

javascript - 在aspx页面中打开 Crystal 报表时出错

c++ - 在 Qt 中用像素图画笔画一条线?