Graphics.ScaleTransform 的替代方案
我正在使用 GDI+ 进行自定义绘图。
通常,如果我想让我绘制的任何内容适合窗口,我会计算适当的比例,然后按该比例对所有内容进行 ScaleTransform:
e.Graphics.ScaleTransform(ratio, ratio);
ScaleTransform 的问题是它会缩放所有内容,包括笔划和画笔。
我要缩放我正在绘制的所有像素坐标吗?每条线、矩形或路径基本上都是一系列点。因此,我可以手动将所有这些点乘以比率,但是有没有一种简单的替代方法可以更无缝地完成此操作?
I am doing custom drawing using the GDI+.
Normally if I want to fit whatever I am drawing to the window, I calculate the appropriate ratio and I ScaleTransform everything by that ratio:
e.Graphics.ScaleTransform(ratio, ratio);
The problem with ScaleTransform is that it scales everything including pen strokes and brushes.
Hoe do I scale all of the pixel coordinates of what I'm drawing? Every line, rectangle, or path is basically a series of points. So I can multiply all of those points by the ratio manually, but is there an easy alternative to do this more seamlessly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试将所有对象放入
GraphicsPath
实例。它没有 ScaleTransform 方法,但您可以使用
GraphicsPath.Transform
。您可以通过Matrix.Scale
。Try putting all your objects in a
GraphicsPath
instance first. It doesn't have aScaleTransform
method but you can transform the objects withGraphicsPath.Transform
. You can pass a scaling matrix viaMatrix.Scale
.您可以包装GDI图形对象并存储比例因子
You can wrap the GDI graphics object and store the scale factor
我认为 ScaleTransform 适用于 GDI 上下文相关的每个数值,因此不幸的是,您不能仅将其用于坐标。 WPF 有一个
GeometryTransform
,但我不知道 GDI+ 中是否有与之等效的函数。如果您担心代码重复,您始终可以编写一个实用方法来绘制形状,并对其点应用一定的比例级别。
您还可以尝试手动反转 ScaleTransform,将其反转应用到您不想缩放的任何对象;我知道有些画笔公开了这种方法。
I think
ScaleTransform
works on every numeric value that the GDI context is concerned with, so you can't just use it for coordinates, unfortunately. WPF has aGeometryTransform
but I don't know of an equivalent to it in GDI+.If you're concerned about code duplication you could always write a utility method to draw the shapes with a certain scale level applied to their points.
You could also try manually reversing the
ScaleTransform
by applying the inverse of it to any objects you don't want scaled; I know some brushes expose this method.幸运的是,Pen 有一个局部 ScaleTransform,通过它可以进行逆缩放以补偿全局变换。
Pen.ResetTransform 在使用下一次之前的每次重新缩放之后,或者当前的笔缩放(与图形上下文无关)可以缩小到几乎为零(实际上,一个像素),射向月球或指向中途。
Fortunately, Pen has a local ScaleTransform by which inverse rescaling can be done to compensate for the global transform.
Pen.ResetTransform after using each rescaling before the next, or the current pen scaling (independent of graphics context) can shrink to nearly nothing (actually, one pixel), shoot to the moon, or points midway.