2009-08-21 69 views
3

无论图形转换矩阵的比例如何,我都需要创建一个宽度为0.5毫米的笔。这是我正在做的:GDI中的非缩放笔+

注意:示例代码是C++/CLI但欢迎使用C#的答案。

// assume that g is a Graphics object 
int dpi = g->DpiX; 
float dotsPerHalfInch = dpi * 0.5; 

// to extract scale factor from the transformation matrix... 
// create two points distant one unit apart... 
array<PointF> ^points = gcnew array<PointF>{ 
    PointF(0.0f, 0.0f), PointF(1.0f, 0.0f) 
}; 

// transform them... 
g->Transform->TransformPoints(points); 

// get distance after transformation... 
float dX = points[1].X - points[0].X; 
float dY = points[1].Y - points[0].Y; 
float distance = Math::Sqrt(dX * dX + dY * dY); 

// reverse the effects of any scale factor in graphics transform 
float penWidth = dotsPerHalfInch/distance; 

Pen ^halfInchPen = gcnew Pen(Color::Black, penWidth); 

似乎并没有在屏幕(96 dpi)的工作......给了我一个像素宽的笔。在PDF打印机上(600 dpi),它使我的笔厚度超过半英寸。

我在做什么错?在GDI中创建非缩放笔的正确方法是什么?

+0

标签C#isn't权。它看起来像C++ – Jehof 2009-08-21 06:42:20

+0

这是C++/CLI ...但欢迎C#答案。 – 2009-08-21 06:55:49

+0

凹凸!颠簸颠簸颠簸! – 2010-02-19 20:13:14

回答

6

笔本身有一个Transform属性。在图形对象上使用与全局缩放相反的变换 - Transform。将笔宽设置为任何你想要的常数。

此外还有一个gdi +制造宽度为< 1.5的笔不能缩放的错误。

很好的链接:http://www.bobpowell.net/scalepens.htm

(也有是对显卡一个PageUnit属性与像素之间变化的能力,毫米等说不上如果能够帮助对象)

+0

链接已死! – 2017-07-17 08:58:53