views:

165

answers:

2

I have a number of graphics objects which I am plotting in a graphics context in a windows forms application. There is some interaction with the ui element in which the paths are rendered which allows the user to pan, zoom and set an origin for the zoom point. The question I have is, is it possible to set up a sequence of transform operations on the graphics object as follows?

[1] Apply a translate transfrom (to shift paths to the origin point for the scale transform) [2] Apply scale transform [3] Apply a translate transform (to shift the path back to the correct location)

It seems I can only order individual transform operations types (translate, scale, etc), so the two translate transforms will not be applied at the correct point (either side of the scale operation). Is there a way to do this? Alternatively, is it possible to set an origin for the scale transform?

I did mess around with nested graphicscontainers, but they didn't seem to help.

Thanks,

Max

+1  A: 

yes. you can. use the Matrix object.

http://en.csharp-online.net/GDIplus_Graphics_Transformation%E2%80%94Matrix_Class_and_Transformation

Benny
+1  A: 

alt text

Code:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Bitmap bmp = new Bitmap(300, 300);
    Graphics g = Graphics.FromImage(bmp);
    System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();

    g.DrawString("this is a string", SystemFonts.DefaultFont,
        Brushes.Black, new Point(50, 50));

    matrix.Rotate(30); // or use RotateAt(...) specifying your rotation point
    g.Transform = matrix;
    g.DrawString("this is a 30 rotated string", SystemFonts.DefaultFont, 
        Brushes.Black, new Point(50, 50));

    matrix.Reset();
    matrix.Translate(50, 50);
    g.Transform = matrix;
    g.DrawString("this is a 50; 50 translated string", SystemFonts.DefaultFont, 
        Brushes.Black, new Point(50, 50));
    pictureBox1.Image = bmp;
}

you can use Matrix to transform GraphicPath or Graphics objects.

serhio