图像应该通过 MouseMove 事件在 PictureBox 中移动

The image is supposed to be moved inside the PictureBox via MouseMove events

在开始一个项目之前,我想事先知道以下是否可行。
该应用程序创建一个 System.Drawing.Bitmap 并在其上绘制。

Bitmap 比 PictureBox 大很多。 Bitmap 和 PictureBox 的高度相同。现在我希望能够通过按下鼠标左键并移动它来在 PictureBox 中(沿着)从左到右移动图像。

见图:

我才发现我还没有回答,我正在努力弥补。 作为解决方案,我使用Jimi的解决方案。我已经缩短了这里的代码以满足我的需要。

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;

namespace Zoom_an_image_from_the_mouse_location
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            string imagePath = "C:\Users\yourPath\Pictures\....jpeg";
            drawingImage = (Bitmap)Image.FromStream(new MemoryStream(File.ReadAllBytes(imagePath)));
            imageRect = new RectangleF(Point.Empty, drawingImage.Size);

            canvas = new PictureBoxEx(new Size(525,700));
            canvas.Location = new Point(10, 10);
            canvas.MouseMove += this.canvas_MouseMove;
            canvas.MouseDown += this.canvas_MouseDown;
            canvas.MouseUp += this.canvas_MouseUp;
            canvas.Paint += this.canvas_Paint;
            this.Controls.Add(canvas);
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            this.BackColor = Color.FromArgb(174, 184, 177);
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
        }

        private float rotationAngle = 0.0f;
        private float zoomFactor = 1.0f;

        private RectangleF imageRect = RectangleF.Empty;
        private PointF imageLocation = PointF.Empty;
        private PointF mouseLocation = PointF.Empty;

        private Bitmap drawingImage = null;
        private PictureBoxEx canvas = null;

        private void canvas_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) return;
            mouseLocation = e.Location;
            imageLocation = imageRect.Location;
            canvas.Cursor = Cursors.NoMove2D;
        }

        private void canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) return;
            imageRect.Location =
                new PointF(imageLocation.X + (e.Location.X - mouseLocation.X),
                           imageLocation.Y); //+ (e.Location.Y - mouseLocation.Y));
            canvas.Invalidate();
        }

        private void canvas_MouseUp(object sender, MouseEventArgs e) =>
            canvas.Cursor = Cursors.Default;

        private void canvas_Paint(object sender, PaintEventArgs e)
        {
            var drawingRect = GetDrawingImageRect(imageRect);

            using (var mxRotation = new Matrix())
            using (var mxTransform = new Matrix())
            {

                e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;

                mxRotation.RotateAt(rotationAngle, GetDrawingImageCenterPoint(drawingRect));
                mxTransform.Multiply(mxRotation);

                e.Graphics.Transform = mxTransform;
                e.Graphics.DrawImage(drawingImage, drawingRect);
            }
        }


        #region Drawing Methods

        public RectangleF GetScaledRect(RectangleF rect, float scaleFactor) =>
            new RectangleF(rect.Location,
            new SizeF(rect.Width * scaleFactor, rect.Height * scaleFactor));

        public RectangleF GetDrawingImageRect(RectangleF rect) =>
            GetScaledRect(rect, zoomFactor);

        public PointF GetDrawingImageCenterPoint(RectangleF rect) =>
            new PointF(rect.X + rect.Width / 2f, rect.Y + rect.Height / 2f);

        #endregion
    }
}

[DesignerCategory("Code")]
public class PictureBoxEx : PictureBox
{
    public PictureBoxEx() : this(new Size(525, 700)) { }
    public PictureBoxEx(Size size)
    {
        SetStyle(ControlStyles.Selectable | ControlStyles.UserMouse, true);
        this.BorderStyle = BorderStyle.FixedSingle;
        this.Size = size;
    }
}