3

I have put this simple code together to draw a line. Now I want to apply a ScaleTransform to it by a factor of 10; but the code below doesn't work.

var bitmap = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height);
var g = Graphics.FromImage(bitmap);
pictureBox1.Image = bitmap;

var pn = new Pen(Color.Wheat, -1);
g.DrawLine(pn, 0, 0, 10, 10);

pn.Dispose();

// I'm trying to scaletransform here!
g.ScaleTransform(10, 10);

Update:

What is the correct way to update the changes? I'm not getting any results from this :(

g.ScaleTransform(1, 1);
pictureBox1.Invalidate();
Vahid
  • 5,144
  • 13
  • 70
  • 146

1 Answers1

3

You must apply the transformation BEFORE drawing the line!

var g = Graphics.FromImage(bitmap);
g.ScaleTransform(10, 10);    
using (pn = new Pen(Color.Wheat, -1)) {
    g.DrawLine(pn, 0, 0, 10, 10);
}

Transformations are applied to the transformation matrix of the graphics object (g.Transform).

Also make use of the using statement in order to dispose the resources. It will even dispose the pen if an exception should occur or if the using statement-block should be left with a return or break statement.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Thanks Olivier, what if I want to change the scale later? For example to implement a Zoom? How can apply ScaleTransform to an already drawn line? – Vahid Mar 24 '14 at 17:31
  • 1
    You cannot change an existing graphic. You always must redraw it! Put the drawing logic in the `Paint` event handler of your picture box and call `pictureBox1.Invalidate()` each time the zoom factor changes (typically in a mouse or key of value-changed event handler). I would draw directly on the picture box instead of the bitmap. – Olivier Jacot-Descombes Mar 24 '14 at 17:35
  • So we don't have something like `ScaleTransform` here that can change the scale of already drawn line. Thanks I'll choose this as the correct answer. I was going to choose WinForms over WPF cause you can set the thicknes to -1 and get a one pixel line all the time and I thought I can scale it without redrawing it to get fast results. I guess I'll move back with WPF. – Vahid Mar 24 '14 at 17:46
  • 1
    Even WPF cannot do that. If you zoom in WPF the whole image will be redrawn by WPF! GDI+ is pretty fast if you do it right. – Olivier Jacot-Descombes Mar 24 '14 at 17:49
  • Actually I'm building a program that will handle CAD files. I'll have maximum 10000 Lines and I will need to Zoom/Pan etc, I was thinking what would be the best choice to implement this. I have reached plausible performance with 3000 lines in WPF, and I'm redrawing on the visual layer, will GDI make much difference in Zoom/Pan? Here is my question regarding this: http://stackoverflow.com/questions/22551841/building-a-cad-program-in-wpf – Vahid Mar 24 '14 at 17:54
  • 1
    WPF is based on DirectX, which is very fast. The fastest way to do it is by drawing directly on the surface in WPF, instead of using WPF line objects. Changing the thickness of 10000 line objects in WPF is not going to be fast. – Olivier Jacot-Descombes Mar 24 '14 at 20:26