【问题标题】:Im trying to use SolidColorBrush but getting many Cannot convert type errors how can i fix it and use the SolidColorBrush?我尝试使用 SolidColorBrush 但遇到很多无法转换类型错误我该如何修复它并使用 SolidColorBrush?
【发布时间】:2013-12-25 22:23:13
【问题描述】:

在form1的顶部我做了:

using System.Windows.Media;

但是在我的代码中我使用笔刷颜色的任何地方我都必须在它之前添加 System.Drawing 例如:

System.Drawing.Pen(System.Drawing.Color.Green, 2f))

如果我不这样做,我会得到一个错误,例如:

System.Drawing.Pen(Color.Green, 2f))

错误 2 'Color' 是 'System.Drawing.Color' 和 'System.Windows.Media.Color' 之间的模糊引用

所以我在它之前添加了 System.Drawing 并且这行没有错误。

问题是当我尝试在我的方法中使用 SolidColorBrush 时,这就是我需要添加 Media 类的原因:

private void DrawText(string text, System.Drawing.Color pen_color, System.Windows.Media.Color brushes_color, Graphics graphics, int point1, int point2, Point point3)
        {
            SolidColorBrush brush = new SolidColorBrush(brushes_color);
            System.Drawing.Color color = ((System.Windows.Media.Brushes)brush).Color;
            using (System.Drawing.Pen pen = new System.Drawing.Pen(pen_color, 6f))
            {
                Point pt1 = new Point(point1); // 369, 90
                Point pt2 = new Point(point2); // 469, 90
                graphics.DrawLine(pen, pt1, pt2);
            }

            graphics.DrawString(text,
                    this.Font, (System.Drawing.Brushes)brush, point3); // 480, 83
        }

我遇到了一些错误:

在这一行:System.Drawing.Brushes

错误 5 参数 3:无法从“System.Drawing.Brushes”转换为“System.Drawing.Brush”

开启:第 3 点

错误 6 参数 4:无法从 'System.Drawing.Point' 转换为 'System.Drawing.RectangleF'

在这一行:(System.Drawing.Brushes)brush

错误 3 无法将类型“System.Windows.Media.SolidColorBrush”转换为“System.Drawing.Brushes”

在这一行:System.Drawing.Color color = ((System.Windows.Media.Brushes)brush).Color;

错误 2 无法将类型“System.Windows.Media.SolidColorBrush”转换为“System.Windows.Media.Brushes”

关于这部分:graphics.DrawString

错误 4 'System.Drawing.Graphics.DrawString(string, System.Drawing.Font, System.Drawing.Brush, System.Drawing.RectangleF)' 的最佳重载方法匹配有一些无效参数

并且属性颜色不存在:System.Windows.Media.Color Brushes_color

错误 1 ​​类型名称“Color”在类型“System.Drawing.Brush”中不存在

我想做的就是让用户能够在这一行选择画笔颜色:

graphics.DrawString(text,
                    this.Font, (System.Drawing.Brushes)brush, point3);

原来的行是:

graphics.DrawString(text,
                    this.Font, Brushes.Green , point3);

但我希望用户选择画笔的颜色。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    问题是当我尝试在我的方法中使用 SolidColorBrush 时,这就是我需要添加 Media 类的原因

    你不需要那个。 System.Drawing 中的类称为SolidBrush。删除 System.Windows.Media 引用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 2019-10-12
      • 1970-01-01
      • 2013-12-13
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多