C# winforms - How to scale the shapes drawn on the picturebox?

Viewed 29

Problem

In general I would like to implement a dxf viewer with drag&drop and zoom in&out features. Currently, I have Panel1 dock fill on Form1, and pictureBox1 is placed on Panel1. When pressing down and moving the left mouse button, changing the location of pictureBox1 to realize the feature of dragging. But I don't know how to implement the zoom feature, I hope you guys can help me modify my code to achieve the zoom feature.

Code

public partial class Form1 : Form
{

    List<Line> dxfLines = new List<Line>();

    System.Drawing.Point mouseDownPosition = new System.Drawing.Point();
    bool isMove = false;
    int zoomStep = 20;

    int mapWidth, mapHeight;


    public Form1()
    {
        InitializeComponent();
    }

    private void toolStripButton1_Click(object sender, EventArgs e)
    {
        Document doc = new Document(dxfFileNameInHere);
        doc.Read();
        dxfLines = doc.Lines;

        foreach (Line line in dxfLines)
        {
            if (line.P1.Y - line.P2.Y == 0)
            {
                int deltaX = (int)Math.Abs(line.P1.X - line.P2.X);
                if (mapWidth == 0)
                {
                    mapWidth = deltaX;
                }
                else if (deltaX > mapWidth)
                {
                    mapWidth = deltaX;
                }
            }
            else if (line.P1.X - line.P2.X == 0)
            {
                int deltaY = (int)Math.Abs(line.P1.Y - line.P2.Y);
                if (mapHeight == 0)
                {
                    mapHeight = deltaY;
                }
                else if (deltaY > mapHeight)
                {
                    mapHeight = deltaY;
                }
            }
        }

        pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
        pictureBox1.Width = mapWidth;
        pictureBox1.Height = mapHeight;
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            mouseDownPosition.X = Cursor.Position.X;
            mouseDownPosition.Y = Cursor.Position.Y;
            isMove = true;
            pictureBox1.Focus();
        }
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        pictureBox1.Focus();
        if (isMove == true)
        {
            int x, y;
            int moveX, moveY;
            moveX = Cursor.Position.X - mouseDownPosition.X;
            moveY = Cursor.Position.Y - mouseDownPosition.Y;
            x = pictureBox1.Location.X + moveX;
            y = pictureBox1.Location.Y + moveY;
            pictureBox1.Location = new System.Drawing.Point(x, y);
            mouseDownPosition.X = Cursor.Position.X;
            mouseDownPosition.Y = Cursor.Position.Y;
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            isMove = false;
        }
    }

    private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
    {
        int x = e.Location.X;
        int y = e.Location.Y;
        int ow = pictureBox1.Width;
        int oh = pictureBox1.Height;
        int VX, VY;
        if (e.Delta > 0)
        {
            pictureBox1.Width += zoomStep;
            pictureBox1.Height += zoomStep;

            PropertyInfo propertyInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
            Rectangle rectangle = (Rectangle)propertyInfo.GetValue(pictureBox1, null);

            pictureBox1.Width = rectangle.Width;
            pictureBox1.Height = rectangle.Height;
        }
        if (e.Delta < 0)
        {
            if (pictureBox1.Width < mapWidth / 10)
            {
                return;
            }
            pictureBox1.Width -= zoomStep;
            pictureBox1.Height -= zoomStep;
            PropertyInfo propertyInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
            Rectangle rectangle = (Rectangle)propertyInfo.GetValue(pictureBox1, null);

            pictureBox1.Width = rectangle.Width;
            pictureBox1.Height = rectangle.Height;
        }
        VX = (int)((double)x * (ow - pictureBox1.Width) / ow);
        VY = (int)((double)y * (oh - pictureBox1.Height) / oh);
        pictureBox1.Location = new System.Drawing.Point(pictureBox1.Location.X + VX, pictureBox1.Location.Y + VY);
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

        foreach(Line line in dxfLines)
        {
            System.Diagnostics.Debug.WriteLine((line.P1.X / 15).ToString());
            e.Graphics.DrawLine(Pens.Red, (float)line.P1.X / 15, (float)line.P1.Y / 15, (float)line.P2.X / 15, (float)line.P2.Y / 15);
        }
    }
}
0 Answers
Related