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. For some reason when i draw a shape with a DashStyle other than solid, it gets drawn in a very unexpected scale.
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.
What managed to fix me this was adding the following line before drawing the shapes:
e.Graphics.ScaleTransform(1, 1);