C# 实现图片的放大缩小和平移

时间:2023-02-05 19:53:52
private void DSApictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (drawLine)
            {
                start = new PointF(e.X,e.Y);
                start = LineAlgorithm.Control2Image(matrix,start);

                if (list1.Count == 0)
                {
                    addLine1 = true;
                }
                else
                {
                    addLine1 = false;
                }
            }
            if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
            {
                if (e.Button == MouseButtons.Left)
                {
                    isZooming = true;
                }
                else
                {
                    isZooming = false;
                }
                if (e.Button == MouseButtons.Right)
                {
                    isPanning = true;
                }
                else
                {
                    isPanning = false;
                }
                startPoint = new System.Drawing.Point(e.X, e.Y);
                drawLine = false;
            }
        }
 private void DSApictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && drawLine)
            {
               
                end = new PointF(e.X,e.Y);
                end = LineAlgorithm.Control2Image(matrix,end);
                if (addLine1)
                {
                    list1.Add(end);
                }
                else
                {
                    list2.Add(end);
                    cursorToolStripMenuItem.Enabled = true;
                   
                }
                DSApictureBox.Refresh();
            }
            if (startPoint.HasValue && drawLine == false)
            {
                int diffX = e.X - startPoint.Value.X;
                int diffY = e.Y - startPoint.Value.Y;

                //Console.WriteLine(string.Format("DiffX {0} DiffY {1}", diffX, diffY));

                if (isPanning)
                {
                    PointF p1 = new PointF(startPoint.Value.X, startPoint.Value.Y);
                    PointF p2 = new PointF(e.X, e.Y);
                    Console.WriteLine(string.Format("p1 {0} {1}", p1.X, p1.Y));
                    Console.WriteLine(string.Format("p2 {0} {1}", p2.X, p2.Y));

                    matrix.Invert();
                    var pts = new PointF[] { p1, p2 };
                    matrix.TransformPoints(pts);

                    Console.WriteLine(string.Format("p1 {0} {1}", pts[0].X, pts[0].Y));
                    Console.WriteLine(string.Format("p2 {0} {1}", pts[1].X, pts[1].Y));

                    matrix.Invert();

                    matrix.Translate(pts[1].X - pts[0].X, pts[1].Y - pts[0].Y);
                }
                if (isZooming)
                {
                    matrix.Scale((300 + diffX) / 300f, (300 + diffX) / 300f);
                }

                startPoint = new System.Drawing.Point(e.X, e.Y);

                DSApictureBox.Refresh();
            }
        }