【问题标题】:Dashed shapes drawing in unexpected scale以意想不到的比例绘制虚线形状
【发布时间】:2012-08-29 12:49:26
【问题描述】:

我正在使用控件绘制事件在我的应用程序中绘制图形对象。所有对象的大小都以毫米为单位存储,因此我使用“毫米”作为图形对象的 PageUnit。出于某种原因,当我使用 DashStyle 而非纯色绘制形状时,它会以非常意外的比例绘制。

在下面的代码示例中,我希望看到两条线一个接一个地绘制,但我得到的是红色虚线在其他地方以更大的比例绘制。

知道我错过了什么吗?

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        private Pen solidBlackPen = new Pen(Color.Black, 1);
        private Pen dashedRedPen = new Pen(Color.Red, 1) { 
                                          DashStyle = DashStyle.Dash 
                                       };

        private Point point1 = new Point(5, 5);
        private Point point2 = new Point(35, 5);

        public Form1()
        {
            InitializeComponent();

            this.BackColor = Color.White;
            this.Paint += new PaintEventHandler(Form1_Paint);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.PageUnit = GraphicsUnit.Millimeter;

            e.Graphics.DrawLine(solidBlackPen, point1, point2);
            e.Graphics.DrawLine(dashedRedPen, point1, point2);
        }

    }
}

由于我是新人,我无法上传屏幕截图。

【问题讨论】:

  • 问题肯定比您的问题中的可用问题更多,因为如果我按照发布的方式执行代码,则这些线条会被绘制在另一个之上。
  • 我猜你是对的。我尝试在具有不同操作系统的不同机器上运行相同的可执行文件,并且在其中一些机器上我确实得到了我期望的结果。谢天谢地,我设法找到了解决这个问题的方法,但我仍然不知道是什么原因造成的。我会发布一个答案,以防其他人遇到这个问题。

标签: c# drawing gdi+ scale shapes


【解决方案1】:

经过几次测试,这在我看来就像某种错误,只发生在特定的操作系统/框架上。

解决这个问题的方法是在绘制形状之前添加以下行:

e.Graphics.ScaleTransform(1, 1);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    相关资源
    最近更新 更多