自制WinForm控件,展示图片功能,支持拖放,移动,放大缩小

效果图如下

picControl

绘制控件代码

using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class MyPicControl : UserControl
    {
        public MyPicControl()
        {
            InitializeComponent();
            this.Controls.Add(myPictureBox);
            myPictureBox.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
            myPictureBox.MouseUp += new MouseEventHandler(pictureBox1_MouseUp);
            myPictureBox.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
            myPictureBox.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);

            this.DragDrop += new DragEventHandler(MyPictureBox_DragDrop);
            this.DragEnter += new DragEventHandler(MyPictureBox_DragEnter);
            this.AllowDrop = true;//是否接收用数据拖放数据到控件上
        }

        /// <summary> 
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 组件设计器生成的代码

        /// <summary> 
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // MyPicControl
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "MyPicControl";
            this.Size = new System.Drawing.Size(754, 609);
            this.ResumeLayout(false);

        }

        #endregion
        public Bitmap GetBitmap
        {
            get { return myBmp; }
            set
            {
                myBmp = value;
                if (myBmp != null)
                {
                    myPictureBox.SizeMode = PictureBoxSizeMode.Zoom; //设置picturebox为缩放模式
                    myPictureBox.Image = myBmp;
                    myPictureBox.Width = myBmp.Width;
                    myPictureBox.Height = myBmp.Height;
                }
            }
        }
        #region 图片展示功能
        PictureBox myPictureBox = new PictureBox();
        Bitmap myBmp;
        Point mouseDownPoint = new Point(); //记录拖拽过程鼠标位置
        bool isMove = false;    //判断鼠标在picturebox上移动时,是否处于拖拽过程(鼠标左键是否按下)
        int zoomStep = 20;      //缩放步长 

        //鼠标按下功能
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseDownPoint.X = Cursor.Position.X;
                mouseDownPoint.Y = Cursor.Position.Y;
                isMove = true;

                myPictureBox.Focus();
            }
        }

        //鼠标松开功能
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMove = false;
            }
        }

        //鼠标移动功能
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {

            if (myBmp == null)
            {
                return;
            }
            myPictureBox.Focus();
            if (isMove)
            {
                int x, y;
                int moveX, moveY;
                moveX = Cursor.Position.X - mouseDownPoint.X;
                moveY = Cursor.Position.Y - mouseDownPoint.Y;
                x = myPictureBox.Location.X + moveX;
                y = myPictureBox.Location.Y + moveY;
                myPictureBox.Location = new Point(x, y);
                mouseDownPoint.X = Cursor.Position.X;
                mouseDownPoint.Y = Cursor.Position.Y;
            }
        }
        //鼠标滚轮滚动功能
        private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            int x = e.Location.X;
            int y = e.Location.Y;
            int ow = myPictureBox.Width;
            int oh = myPictureBox.Height;
            int VX, VY;
            if (e.Delta > 0)
            {
                myPictureBox.Width += zoomStep;
                myPictureBox.Height += zoomStep;

                PropertyInfo pInfo = myPictureBox.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
                    BindingFlags.NonPublic);
                Rectangle rect = (Rectangle)pInfo.GetValue(myPictureBox, null);

                myPictureBox.Width = rect.Width;
                myPictureBox.Height = rect.Height;
            }
            if (e.Delta < 0)
            {

                if (myPictureBox.Width < myBmp.Width / 10)
                    return;

                myPictureBox.Width -= zoomStep;
                myPictureBox.Height -= zoomStep;
                PropertyInfo pInfo = myPictureBox.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
                    BindingFlags.NonPublic);
                Rectangle rect = (Rectangle)pInfo.GetValue(myPictureBox, null);
                myPictureBox.Width = rect.Width;
                myPictureBox.Height = rect.Height;
            }

            VX = (int)((double)x * (ow - myPictureBox.Width) / ow);
            VY = (int)((double)y * (oh - myPictureBox.Height) / oh);
            myPictureBox.Location = new Point(myPictureBox.Location.X + VX, myPictureBox.Location.Y + VY);
        }
        #endregion


        private void MyPictureBox_DragDrop(object sender, DragEventArgs e)
        {
            string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            string extension = System.IO.Path.GetExtension(path);
            string[] extensions = new string[] { ".PNG", ".JPG" };
            if (extensions.Contains(extension.ToUpper()))
            {
                myBmp = new Bitmap(path);
                myPictureBox.SizeMode = PictureBoxSizeMode.Zoom; //设置picturebox为缩放模式
                myPictureBox.Image = myBmp;
                myPictureBox.Width = myBmp.Width;
                myPictureBox.Height = myBmp.Height;
            }
            else
            {
                MessageBox.Show($"不支持{extension}格式文件");
            }
        }

        private void MyPictureBox_DragEnter(object sender, DragEventArgs e)
        {
            //只允许文件拖放
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
    }
}

显示调用方法代码

/// <summary>
/// 控件的点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.Filter = "全部(*.*)|*.*";
    if (dlg.ShowDialog() == DialogResult.OK)
    {
        Bitmap myBmp = new Bitmap(dlg.FileName);
        myPicControl1.GetBitmap = myBmp;//myPicControl1  这个为自制控件 已在主窗体添加
    }
}