Doing some GIS programming I stumbled over the following strange issue:
When I apply a transformation matrix with fairly large transformation values to a Graphics object and then draw a line from point A to point B, it is drawn crooked as soon as I apply a DashStyle to the used Pen object.
Here is some sample code:
protected override void OnPaint( PaintEventArgs e ) {
base.OnPaint( e );
Graphics g = e.Graphics;
g.Clear( Color.White );
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Matrix m = new System.Drawing.Drawing2D.Matrix();
m.Reset();
m.Translate( -1520106.88f, -6597594.5f, MatrixOrder.Append );
m.Scale( 0.393068463f, -0.393068463f, MatrixOrder.Append );
g.Transform = m;
g.TranslateTransform( 0, 400 );
Pen p1 = new Pen( Color.Green, 8.0f );
Pen p2 = new Pen( Color.Magenta, 7.0f );
PointF[] roadLine = new PointF[2];
roadLine[0] = new PointF( 1520170.13f, 6596633.0f );
roadLine[1] = new PointF( 1521997.38f, 6596959.0f );
// Normal (solid line)
g.DrawLines( p1, roadLine );
g.DrawLines( p2, roadLine );
g.TranslateTransform( 0, -200 );
//Dashed
g.DrawLines( p1, roadLine );
p2.DashStyle = DashStyle.Dash;
g.DrawLines( p2, roadLine );
g.TranslateTransform( 0, -200 );
//Dashed
g.DrawLines( p1, roadLine );
p2.DashStyle = DashStyle.DashDot;
g.DrawLines( p2, roadLine );
g.TranslateTransform( 0, -200 );
//Dash-Dot-Dot
g.DrawLines( p1, roadLine );
p2.DashStyle = DashStyle.DashDotDot;
g.DrawLines( p2, roadLine );
}
If you put this in a new Windows Forms app and run it, you’ll see some solid lines and some dashed lines (with different dash styles). I would expect the dashed lines would completely overlay the respective solid line, but they don’t.
Note that both the green, solid lines and the dashed lines use the coordinates for point A and point B.
Is this expected behaviour, or should I report this as a .NET bug?
I tested with Visual Studio 2008 and .NET 2.0 and .NET 3.5.
Yes, it’s a bug. Should you report it? I don’t think Microsoft is going to do anything about it.
You can try changing your graphics routine to try and not work on such a scale or just avoid using the
DashStyleproperty. Setting your ownDashPatterndoesn’t solve the problem either.I think WPF would handle this situation better, since it’s vector based, not pixel based.