Maybe a C# bug
-
I try this code by overriding control's OnPaint method:
Pen p = new Pen(Color.Red, UnitConverter.PixelToInch(1));
p.DashStyle = DashStyle.Dot;
g.DrawLine(p, 1, 1, 50, 1);The result seems like it draws a dot with 1 inch width, like this:
__________ __________ __________ __________
but if you change the pen width to 2 pixels (means 0.00xx inch of course), we have the correct result:
...................................................................
(in 2 pixel width). am I misunderstand the .NET method to draw line or is it a bug? Please help me to solve this problem, thank for you help.
-
I try this code by overriding control's OnPaint method:
Pen p = new Pen(Color.Red, UnitConverter.PixelToInch(1));
p.DashStyle = DashStyle.Dot;
g.DrawLine(p, 1, 1, 50, 1);The result seems like it draws a dot with 1 inch width, like this:
__________ __________ __________ __________
but if you change the pen width to 2 pixels (means 0.00xx inch of course), we have the correct result:
...................................................................
(in 2 pixel width). am I misunderstand the .NET method to draw line or is it a bug? Please help me to solve this problem, thank for you help.
-
The question is what is the implementation of
UnitConverter.PixelToInch(1)
because I cant find that anywhere in the framework. However, I have tried many values for the 2nd parameter ofPen
and I always seemto get the right result.It's the digital Dutch unit converter[^] of course :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
The question is what is the implementation of
UnitConverter.PixelToInch(1)
because I cant find that anywhere in the framework. However, I have tried many values for the 2nd parameter ofPen
and I always seemto get the right result.I'm sorry not to show you enough information. The first thing I forget is the PageUnit change for graphics object
Graphics g = e.Graphics;
g.PageUnit = GraphicsUnit.Inch;
Pen p = new Pen(Color.Red, UnitConverter.PixelToInch(1));
p.DashStyle = DashStyle.Dot;
g.DrawLine(p, 1, 1, 50, 1);The second: UnitConvert is just a class I write to change the pen width:
public static class UnitConverter
{
static float _dpi = 96;
public static float Dpi
{
get { return UnitConverter._dpi; }
set { UnitConverter._dpi = value; }
}public static float PixelToUnit(double pixel, GraphicsUnit unit) { double result = 0; switch (unit) { case GraphicsUnit.Inch: result = pixel / Dpi; break; case GraphicsUnit.Pixel: result = pixel; break; case GraphicsUnit.Millimeter: result = pixel / (Dpi / 2.54) \* 10; break; case GraphicsUnit.Point: result = pixel / (Dpi / 72); break; } return (float)result; } }
}
-
I'm sorry not to show you enough information. The first thing I forget is the PageUnit change for graphics object
Graphics g = e.Graphics;
g.PageUnit = GraphicsUnit.Inch;
Pen p = new Pen(Color.Red, UnitConverter.PixelToInch(1));
p.DashStyle = DashStyle.Dot;
g.DrawLine(p, 1, 1, 50, 1);The second: UnitConvert is just a class I write to change the pen width:
public static class UnitConverter
{
static float _dpi = 96;
public static float Dpi
{
get { return UnitConverter._dpi; }
set { UnitConverter._dpi = value; }
}public static float PixelToUnit(double pixel, GraphicsUnit unit) { double result = 0; switch (unit) { case GraphicsUnit.Inch: result = pixel / Dpi; break; case GraphicsUnit.Pixel: result = pixel; break; case GraphicsUnit.Millimeter: result = pixel / (Dpi / 2.54) \* 10; break; case GraphicsUnit.Point: result = pixel / (Dpi / 72); break; } return (float)result; } }
}
Duong Tien Nam wrote:
UnitConverter.PixelToInch
Duong Tien Nam wrote:
public static float PixelToUnit(double pixel, GraphicsUnit unit)
Your code still doesnt compile, but I see what you're doing now. I added this method to UnitConverter:
public static float PixelToInch(double pixel)
{
return PixelToUnit(pixel, GraphicsUnit.Inch);
}I get the same result you do. However I notice that this line:
g.PageUnit = GraphicsUnit.Inch;
if left as Pixel produces the correct results. Remember that you're calculating Picels from an Inch value, so no need to set the PageUnit to Inches. If you want to set it to inches you may as well not have the UnitConverter stuff in there:Graphics g = e.Graphics;
g.PageUnit = GraphicsUnit.Inch;
Pen p = new Pen(Color.Red, 1F);
p.DashStyle = DashStyle.Dot;
g.DrawLine(p, 1, 1, 500, 1); -
Duong Tien Nam wrote:
UnitConverter.PixelToInch
Duong Tien Nam wrote:
public static float PixelToUnit(double pixel, GraphicsUnit unit)
Your code still doesnt compile, but I see what you're doing now. I added this method to UnitConverter:
public static float PixelToInch(double pixel)
{
return PixelToUnit(pixel, GraphicsUnit.Inch);
}I get the same result you do. However I notice that this line:
g.PageUnit = GraphicsUnit.Inch;
if left as Pixel produces the correct results. Remember that you're calculating Picels from an Inch value, so no need to set the PageUnit to Inches. If you want to set it to inches you may as well not have the UnitConverter stuff in there:Graphics g = e.Graphics;
g.PageUnit = GraphicsUnit.Inch;
Pen p = new Pen(Color.Red, 1F);
p.DashStyle = DashStyle.Dot;
g.DrawLine(p, 1, 1, 500, 1);The reason I have to change the graphics' PageUnit to inch because I want all to draw in inch measurement. If you test you code above, you will see that it draws a dotted line with 1 inch width. So you need to convert 1 inch width pen to 1 pixel with pen with PixelToInch method above. I hope you can understand my explanation because English is not native language. Thank you.