龙空技术网

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

快乐鲤渔 761

前言:

今天大家对“winform界面控件”可能比较关怀,朋友们都想要了解一些“winform界面控件”的相关知识。那么小编也在网上汇集了一些有关“winform界面控件””的相关知识,希望朋友们能喜欢,看官们快快来了解一下吧!

声明变量与初始化:

        private Point lastLocation;//全局变量        private bool isMoving = false;        private Bitmap myBmp;        public Form1()        {            InitializeComponent();            this.MouseWheel += Form1_MouseWheel;                   }
打开与另存图片:
        private void button1_Click_1(object sender, EventArgs e)//选择要打开的图片        {            OpenFileDialog openFileDialog = new OpenFileDialog();            openFileDialog.Filter = "Image Files|*.jpg;*.png";            if (openFileDialog.ShowDialog() == DialogResult.OK)            {                myBmp = new Bitmap(openFileDialog.FileName);                pictureBox1.Image = myBmp;            }        }        private void button2_Click(object sender, EventArgs e)//图片另存        {            SaveFileDialog saveFileDialog1 = new SaveFileDialog();            saveFileDialog1.Filter = "JPEG Image|*.jpg|PNG Image|*.png|BMP Image|*.bmp";            saveFileDialog1.Title = "Save Image";            saveFileDialog1.ShowDialog();            if (saveFileDialog1.FileName != "")            {                // 保存图片                pictureBox1.Image.Save(saveFileDialog1.FileName);            }        }
图片平移到任意位置:
       private void pictureBox1_MouseDown(object sender, MouseEventArgs e)        {            if (e.Button == MouseButtons.Left)            {                isMoving = true;                lastLocation = e.Location;            }        }       private void pictureBox1_MouseMove(object sender, MouseEventArgs e)        {            if (isMoving)            {                int deltaX = e.X - lastLocation.X;                int deltaY = e.Y - lastLocation.Y;                pictureBox1.Left += deltaX;                pictureBox1.Top += deltaY;            }        }       private void pictureBox1_MouseUp(object sender, MouseEventArgs e)        {            if (e.Button == MouseButtons.Left)            {                isMoving = false;            }        }
图片随意放大或缩小:
       private void Form1_MouseWheel(object sender, MouseEventArgs e)        {            Rectangle rectang = new Rectangle();            rectang = panel1.RectangleToClient(this.ClientRectangle);            if (rectang.Contains(MousePosition)) //判断鼠标位置是否在图片区域内            {                double st = 1.5;//缩放倍率                if (e.Delta > 0)//放大                {                    if (pictureBox1.Height < Screen.PrimaryScreen.Bounds.Height * 9)                    {                        pictureBox1.Size = new Size((int)(pictureBox1.Width * st), (int)(pictureBox1.Height * st));//调整picturebox大小                        int px = (int)(pictureBox1.PointToClient(MousePosition).X * (st - 1.0));                        int py = (int)(pictureBox1.PointToClient(MousePosition).Y * (st - 1.0));                        pictureBox1.Location = new Point(pictureBox1.Location.X - px, pictureBox1.Location.Y - py);//调整picturebox位置                    }                }                else//缩小                {                    pictureBox1.Size = new Size((int)(pictureBox1.Width / st), (int)(pictureBox1.Height / st));                    int px = (int)(pictureBox1.PointToClient(MousePosition).X * (1.0 - 1.0 / st));                    int py= (int)(pictureBox1.PointToClient(MousePosition).Y * (1.0 - 1.0 / st));                    pictureBox1.Location = new Point(pictureBox1.Location.X + px, pictureBox1.Location.Y + py);                                   }            }        }          

#头条文章养成计划##医生家中搜出1.5亿?医院回应#

标签: #winform界面控件 #winform代码添加控件