Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Pen object scaling issue
 

Pen object scaling issue

Hi,

I am drawing on a pre-scaled Graphicsobject (scaled using g.ScaleTransform(sx, sy)) with a Pen object.Obviously,I don't want the Penobject to be affected by that scaling, so I am doing a reverse scaling on my Pen object using p.ScaleTransform(1/sx, 1/sy). This works perfectly and the pen width looks good in both x and y dimensions. The only problem i am facing is that when the DashStyle property of the pen is set to Dash, the resulting dashes are scaled differently inx and y axis. For example, a 200 x 200 pixel Rectangle may have 6 dashes width-wise, while only 4 dashes height-wise.

Even custom pen styles don't do any good. Can anyone guide me what's going on? Thanks in advance.

Syed Shujaat Hussain  Saturday, October 06, 2007 10:18 AM

Hi, Syed,

I think this issue is not related to the Scaling, but the drawing process of Pen.

When you are drawing a rectangle with Pen object,

it draws 4 lines clockwiseas a matter of fact.

And if the last dash of one side is too long,

it will be trimmed and continued in next side.

For example,

Code Block

private void Form1_Paint(object sender, PaintEventArgs e)

{

Pen pen = new Pen(Brushes.Red,2);

pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; e.Graphics.ScaleTransform(5, 5); pen.ScaleTransform(1f / 5f, 1f / 5f); e.Graphics.DrawRectangle(pen, 10, 10, 36, 36);

}

Code Block

private void Form1_Paint(object sender, PaintEventArgs e)

{

Pen pen = new Pen(Brushes.Red,2);

pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

pen.DashPattern = new float[2] { 2.5f, 2f };//2.5 for dash, 2 for blank

e.Graphics.ScaleTransform(5, 5);

pen.ScaleTransform(1f / 5f, 1f / 5f);

e.Graphics.DrawRectangle(pen, 10, 10, 36, 36);

}

http://msdn2.microsoft.com/en-us/library/system.drawing.pen.dashpattern.aspx

Hope this helps,

Regards

Yu Guo â€?MSFT  Friday, October 12, 2007 2:38 AM

According to Bob Powell there is bug that prevents brushes of size 1.5 and below from scalling correctly.

http://www.bobpowell.net/scalepens.htm

Ken Tucker  Monday, October 08, 2007 12:48 AM
Thanks for the post Ken, but Bob Powell seems to be pointing to the Pen width and not pen style. Also I am testing things on quite large pen widths like 16, 32 etc, and the effect is visible at that width too. So I think this may not be related to that.
Syed Shujaat Hussain  Monday, October 08, 2007 5:45 AM

Hi, Syed,

I think this issue is not related to the Scaling, but the drawing process of Pen.

When you are drawing a rectangle with Pen object,

it draws 4 lines clockwiseas a matter of fact.

And if the last dash of one side is too long,

it will be trimmed and continued in next side.

For example,

Code Block

private void Form1_Paint(object sender, PaintEventArgs e)

{

Pen pen = new Pen(Brushes.Red,2);

pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; e.Graphics.ScaleTransform(5, 5); pen.ScaleTransform(1f / 5f, 1f / 5f); e.Graphics.DrawRectangle(pen, 10, 10, 36, 36);

}

Code Block

private void Form1_Paint(object sender, PaintEventArgs e)

{

Pen pen = new Pen(Brushes.Red,2);

pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

pen.DashPattern = new float[2] { 2.5f, 2f };//2.5 for dash, 2 for blank

e.Graphics.ScaleTransform(5, 5);

pen.ScaleTransform(1f / 5f, 1f / 5f);

e.Graphics.DrawRectangle(pen, 10, 10, 36, 36);

}

http://msdn2.microsoft.com/en-us/library/system.drawing.pen.dashpattern.aspx

Hope this helps,

Regards

Yu Guo â€?MSFT  Friday, October 12, 2007 2:38 AM

You can use google to search for other answers

Custom Search

More Threads

• No overload for method 'ExecuteNonQuery' takes '1' arguments
• Databinding question - How to databind the Text in a Textbox to a particular cell in an array?
• Concurrency violation error
• Restoring my DataGridViewComboBoxColumns when the values in one column are a subset of another.
• DataBinding to a ListBox at runtime?
• Retrieve all the installed SQL server istances
• Error Datagridview binding with Date
• Data Binding to a simple entity object breaks after update from webservice
• set the background color for a row in a datagridview
• How to syncronize DataBindings?