Alternative to Graphics.ScaleTransform

I am doing a custom drawing using GDI +.

Usually, if I want to fit everything that I draw into a window, I calculate the corresponding ratio and scale everything, transforming this ratio:

e.Graphics.ScaleTransform(ratio, ratio);

The problem with ScaleTransform is that it scales everything, including strokes and brushes.

How do I scale all the pixel coordinates of what I'm drawing? Each line, rectangle or path is basically a sequence of points. So I can multiply all these points by the ratio manually, but is there a simple alternative to make this more smooth?

+5
source share
4 answers

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
}
+1
+2

, ScaleTransform , GDI, , . WPF GeometryTransform, GDI +.

, , .

ScaleTransform, , ; , .

+1

Fortunately, Pen has a local ScaleTransform that can be used to scale back to compensate for global transformation. Pen.ResetTransform after using each zoom to the next or current pen scaling (regardless of the graphics context) can be compressed to almost zero (in fact, one pixel), shoot at the moon or halfway.

0
source

All Articles