簡體   English   中英

虛線形狀以意外的比例繪制

[英]Dashed shapes drawing in unexpected scale

我正在使用控件繪制事件在應用程序中繪制圖形對象。 所有對象的大小都以毫米為單位存儲,因此我將“毫米”用作圖形對象的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);
        }

    }
}

由於我是新手,所以我無法上傳屏幕截圖。

經過幾次測試,在我看來這就像只在特定的OS /框架上發生的bug。

解決這個問題的方法是在繪制形狀之前添加了以下行:

e.Graphics.ScaleTransform(1, 1);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM