我想给一个TextBox添加水印。TextBox.Background是System.Windows.Media.Brush类型的,我需要使用Graphics.FillRectangle(System.Drawing.Brush....)方法。
有没有办法将media brush转换为drawing brush?
我想给一个TextBox添加水印。TextBox.Background是System.Windows.Media.Brush类型的,我需要使用Graphics.FillRectangle(System.Drawing.Brush....)方法。
有没有办法将media brush转换为drawing brush?
试着用这个
System.Drawing.Brush b = new System.Drawing.SolidBrush((System.Drawing.Color)new System.Drawing.ColorConverter().ConvertFromString(new System.Windows.Media.BrushConverter().ConvertToString(mediabrush)));
System.Drawing.ColorTranslator.FromHtml("hexcolor");
或者像我这样使用 (System.Drawing.Color)new System.Drawing.ColorConverter().ConvertFromString("hexcolor");
来转换颜色。 - Samvar newBrush = new System.Drawing.SolidBrush(brush.ToSystemDrawingColor())
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);
}