首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#如何将system.windows.media.brush转换为system.drawing.brush

C#如何将system.windows.media.brush转换为system.drawing.brush
EN

Stack Overflow用户
提问于 2013-10-24 15:46:46
回答 2查看 7.1K关注 0票数 5

我正试图为一个TextBox添加水印。TextBox.Background是System.Windows.Media.Brush。我需要Graphics.FillRectangle(System.Drawing.Brush....)

有没有办法把中间画笔转换成画笔?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-24 16:30:20

试试这个

代码语言:javascript
复制
System.Drawing.Brush b = new System.Drawing.SolidBrush((System.Drawing.Color)new System.Drawing.ColorConverter().ConvertFromString(new System.Windows.Media.BrushConverter().ConvertToString(mediabrush)));
票数 10
EN

Stack Overflow用户

发布于 2022-04-17 02:52:11

这也适用于UWP和WinUI。用法:

代码语言:javascript
复制
var newBrush = new System.Drawing.SolidBrush(brush.ToSystemDrawingColor())
代码语言:javascript
复制
internal static System.Drawing.Color ToSystemDrawingColor(this Brush? brush)
{
    if (brush == null)
    {
        return System.Drawing.Color.Transparent;
    }

    if (brush is not SolidColorBrush solidColorBrush)
    {
        throw new NotImplementedException();
    }

    var color = solidColorBrush.Color;

    return System.Drawing.Color.FromArgb(
        alpha: color.A,
        red: color.R,
        green: color.G,
        blue: color.B);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19570593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档