Skip to content

gdi bug in System.Drawing.Drawing2D?


Am I the only one who ran into .NET (or GDI?) bug with Pen (or Matrix) .ScaleTransform()?
When I scale a pen to certain width, and it has an anchor, anchor draws at inverse scale.
I might be missing something, but here’s my test. Pen scales proportionally to form width, for simplicity.

Here’s how it looks:

You see, the thinner the line, the bigger is the cap. Uh-oh.

My workaround should be here after some time.
Source code for interested ones:

private void Form1_Load(object sender, EventArgs e)
        {
            pen.StartCap = LineCap.SquareAnchor;
            //pen.EndCap = LineCap.ArrowAnchor;
            pen.EndCap = LineCap.DiamondAnchor;
        }

        private readonly Pen pen = new Pen(Color.Black, 2);

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            pen.ResetTransform();
            float scale = Width / 300.0F;
            pen.ScaleTransform(scale, scale);
            e.Graphics.DrawLine(pen, 10, 10, ClientRectangle.Width - 10, ClientRectangle.Height - 10);
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            Invalidate();
        }

Post a Comment

Your email is never published nor shared.