0

i have to ZOOM-IN & ZOOM-OUT specific portion of 2D-graphics like (line, Rectangle, circle) which i have drawn on winform. I am not used any picture box, panel. I crated simple program to draw circle & on button click try to zoom but it showing error "parameter is not valid" in method Drawing() @ Line- DeviceContexct.Transform = mainViewTransform;

public Graphics DeviceContexct;
  public Matrix mainViewTransform = new Matrix();

    private void ScalingCircle_Paint( object sender, PaintEventArgs e )
            {
                Pen myPen = new Pen(Color.Blue, 1); 
                e.Graphics.DrawRectangle(myPen, 50, 50, 100, 100);           
                mainViewTransform.Scale(3, 2);
                DeviceContexct = e.Graphics;
            }
     private void Drawing(Graphics gr)
            {
                Pen myPen2 = new Pen(Color.Red, 1);
                DeviceContexct.Transform = mainViewTransform;
                DeviceContexct.DrawRectangle(myPen2, 50, 50, 100, 100);
            }
       private void button1_Click( object sender, EventArgs e )
            {
                Drawing(DeviceContexct);         

            }
  • 1
    If you're asking how to do it (or to get ready-to-use code) then this is off-topic. If you have a specific problem with code you wrote then please include it here. – Adriano Repetti Jan 10 '18 at 10:59
  • i want some hint not all ready made code actually i didn't find any proper solution.there are code on base of panel & picturebox etc. – user8851697 Jan 10 '18 at 11:08
  • 1
    Are you drawing those object by yourself in `Paint` event? Check `Graphics.ScaleTransform()` and `Graphics.TranslateTransform()` methods (it's much easier than recalculating vertices according to pan/zoom data) – Adriano Repetti Jan 10 '18 at 11:21
  • see https://stackoverflow.com/a/37269366/2521214 – Spektre Jan 10 '18 at 13:33

3 Answers3

0

I prefer to draw into Bitmaps with System.Drawing.Graphics object. Then you can use your graphics object with "go.DrawImage(...)" to draw the bitmap directly into your winforms and actually give it a scale.

https://msdn.microsoft.com/en-us//library/ms142040(v=vs.110).aspx

Rico
  • 1
0

I got the solution, thanks for your help.

I only need to call refresh/invalidate etc on button click.

public partial class ScalingCircle : Form
{
    public Graphics DeviceContexct;
    // current transformation matrix of main view (offset & scaling)
    public Matrix mainViewTransform = new Matrix();

    public int scale = 1;

    public ScalingCircle()
    {
        InitializeComponent();
        DeviceContexct = Graphics.FromHwnd(this.Handle);
        DeviceContexct = this.CreateGraphics();
    }

    public void ScalingCircle_Paint(object sender, PaintEventArgs e)
    {
        DeviceContexct = e.Graphics;
        DeviceContexct.PageUnit = GraphicsUnit.Pixel;
        DeviceContexct.Transform = mainViewTransform;
        ScalingCircle1(scale);
    }

    private void ScalingCircle1(int x )
    {
        Pen myPen2 = new Pen(Color.Black, 1);
        DeviceContexct.Transform = mainViewTransform;

        Rectangle myRectangle = new Rectangle(50, 50, 100 * x, 100 * x);
        DeviceContexct.FillRectangle(new SolidBrush(Color.BurlyWood), myRectangle);
    }

    private void ScalingCircle_Load( object sender, EventArgs e )
    {
        this.ResizeRedraw = true;
      }

    private void button1_Click( object sender, EventArgs e )
    {
        scale += 5;
        this.Refresh();
    }

    private void button2_Click( object sender, EventArgs e )
    {
        if (scale > 1)
        {
            scale -= 5;
            this.Refresh();
        }
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
  • No, there are many serious drawbacks with this approach (even if your task is EXACTLY what we see here). Feel free to post this code on Code Review, if you want to. – Adriano Repetti Jan 12 '18 at 07:05
  • yes , you can review code. & if there is any suggestion you can give me. @Adriano Repetti – user8851697 Jan 12 '18 at 08:22
  • I mean, you may post it on https://codereview.stackexchange.com/. I don't know _who_ will review it but you may get some useful feedback. If you will then don't forget to include the full source code (without designer generated stuff...) and a description of what the application is supposed to do. – Adriano Repetti Jan 12 '18 at 08:29
-2

You can use transformations for that. The graphics object you use for painting things has a Transformation property of type System.Drawing.Drawing2D.Matrix. Graphics also has the ScaleTransform method.

I haven't used it myself, but that is how microsofts Chart does it.

https://msdn.microsoft.com/de-de/library/system.drawing.drawing2d.matrix(v=vs.110).aspx https://msdn.microsoft.com/de-de/library/system.drawing.graphics.scaletransform(v=vs.110).aspx

Otterprinz
  • 459
  • 3
  • 10