Graphics.ScaleTransform 的替代方案

发布于 2024-12-09 22:22:35 字数 299 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

很酷又爱笑 2024-12-16 22:22:35

尝试将所有对象放入 GraphicsPath实例。它没有 ScaleTransform 方法,但您可以使用 GraphicsPath.Transform。您可以通过 Matrix.Scale

Try putting all your objects in a GraphicsPath instance first. It doesn't have a ScaleTransform method but you can transform the objects with GraphicsPath.Transform. You can pass a scaling matrix via Matrix.Scale.

意犹 2024-12-16 22:22:35

您可以包装GDI图形对象并存储比例因子

interface IDrawing
{
   void Scale(float sx, float sy);
   void Translate(float dx, float dy);
   void SetPen(Color col, float thickness);
   void DrawLine(Point from, Point to);
   // ... more methods
}

class GdiPlusDrawing : IDrawing
{
   private float scale;
   private Graphics graphics;
   private Pen pen;

   public GdiPlusDrawing(Graphics g)
   {
      float scale = 1.0f;
   }

   public void Scale(float s)
   {
       scale *= s;
       graphics.ScaleTransform(s,s);
   }

   public void SetPen(Color color, float thickness)
   {
       // Use scale to compensate pen thickness.
       float penThickness = thickness/scale;
       pen = new Pen(color, penThickness);   // Note, need to dispose.
   }

   // Implement rest of IDrawing
}

You can wrap the GDI graphics object and store the scale factor

interface IDrawing
{
   void Scale(float sx, float sy);
   void Translate(float dx, float dy);
   void SetPen(Color col, float thickness);
   void DrawLine(Point from, Point to);
   // ... more methods
}

class GdiPlusDrawing : IDrawing
{
   private float scale;
   private Graphics graphics;
   private Pen pen;

   public GdiPlusDrawing(Graphics g)
   {
      float scale = 1.0f;
   }

   public void Scale(float s)
   {
       scale *= s;
       graphics.ScaleTransform(s,s);
   }

   public void SetPen(Color color, float thickness)
   {
       // Use scale to compensate pen thickness.
       float penThickness = thickness/scale;
       pen = new Pen(color, penThickness);   // Note, need to dispose.
   }

   // Implement rest of IDrawing
}
天荒地未老 2024-12-16 22:22:35

我认为 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 a GeometryTransform 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.

陪你到最终 2024-12-16 22:22:35

幸运的是,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文