简体   繁体   English

虚线形状以意外的比例绘制

[英]Dashed shapes drawing in unexpected scale

I am using a control paint event to draw graphic objects in my application. 我正在使用控件绘制事件在应用程序中绘制图形对象。 The sizes of all the object is stored with size unit of millimeters and therefore i use 'millimeter' as PageUnit for the graphic object. 所有对象的大小都以毫米为单位存储,因此我将“毫米”用作图形对象的PageUnit。 For some reason when i draw a shape with a DashStyle other than solid, it gets drawn in a very unexpected scale. 由于某些原因,当我使用非实心的DashStyle绘制形状时,它以非常出乎意料的比例绘制。

In the code example below I expect to see both lines being drawn one on top of the other, but what i get is the red dashed line being drawn elsewhere in larger scale. 在下面的代码示例中,我希望看到两条线都在另一条线的上方绘制,但是我得到的是红色虚线在其他地方较大比例地绘制。

Any idea what I am missing? 知道我缺少什么吗?

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);
        }

    }
}

Since I am new i cant upload a screenshot. 由于我是新手,所以我无法上传屏幕截图。

After few tests this looks to me like some kind of bug that occures mybe only on specific Os/framework. 经过几次测试,在我看来这就像只在特定的OS /框架上发生的bug。

What managed to fix me this was adding the following line before drawing the shapes: 解决这个问题的方法是在绘制形状之前添加了以下行:

e.Graphics.ScaleTransform(1, 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM