WinForm图片的移动缩放

图片的移动:

         /// <summary>
        /// 记录鼠标是否按下了
        /// </summary>
        private bool isMouseDown;

        /// <summary>
        /// 记录 鼠标按下时的x,y坐标
        /// </summary>
        private Point mouseDownPoint;    

        //图片控件上 按下鼠标
        private void picbox_ProductPic_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseDownPoint.X = Cursor.Position.X;
                mouseDownPoint.Y = Cursor.Position.Y;
                isMouseDown = true;
            }
        }

        //图片控件上 移动鼠标
        private void picbox_ProductPic_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                this.picbox_ProductPic.Left = this.picbox_ProductPic.Left + (Cursor.Position.X - mouseDownPoint.X);//移动图片,Left = Left + 当前鼠标点 - 鼠标点击时候的点
                this.picbox_ProductPic.Top = this.picbox_ProductPic.Top + (Cursor.Position.Y - mouseDownPoint.Y);//移动图片
                mouseDownPoint.X = Cursor.Position.X;//更新鼠标点坐标
                mouseDownPoint.Y = Cursor.Position.Y;//更新鼠标点坐标
            }
        }

        //图片控件上 释放鼠标
        private void picbox_ProductPic_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }

  图片的缩放

private Image image_ori;//原始图片
private int zoomtime=0;//缩放的次数

this.MouseWheel+=new MouseEventHandler(picbox_ProductPic_MouseWheel);//滚轮监听事件
        private void picbox_ProductPic_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            int numberOfTextLinesToMove = 0;
            numberOfTextLinesToMove = e.Delta * SystemInformation.MouseWheelScrollLines / 120;

            if (cb_Zoom.Checked == false) numberOfTextLinesToMove = 0;

            if (numberOfTextLinesToMove > 0)
            {
                for (int i = 0; i < numberOfTextLinesToMove; i++)
                {
                    zoomtime++;
                    OperateClass.maxMin(picbox_ProductPic, image_ori, zoomtime);


                }
            }
            else if (numberOfTextLinesToMove < 0)
            {
                for (int i = 0; i > numberOfTextLinesToMove; i--)
                {
                    zoomtime--;
                    OperateClass.maxMin(picbox_ProductPic, image_ori, zoomtime);
                }
            }
        }

        //鼠标进入控件的可见部分
        private void picbox_ProductPic_MouseEnter(object sender, EventArgs e)
        {
            if (cb_Zoom.Checked == true)
            {
                picbox_ProductPic.Focus();
                OperateClass.resetPic(picbox_ProductPic);
            }
        }

用到的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace _009_TPS.MyClass
{ 
    /// <summary>
    /// 图片操作的类
    /// </summary>
    class OperateClass
    {

        public static void SetSuitPictureBox(PictureBox picBox, Panel pan_picture)
        {
            picBox.Dock = DockStyle.Fill;
            picBox.SizeMode = PictureBoxSizeMode.Zoom;
            int x, y, height, width, vmax, hmax;
            x = pan_picture.HorizontalScroll.LargeChange;
            y = pan_picture.VerticalScroll.LargeChange;
            height = pan_picture.Height;
            width = pan_picture.Width;
            vmax = pan_picture.HorizontalScroll.Maximum;
            hmax = pan_picture.VerticalScroll.Maximum;
            try
            {

                Image image = Image.FromFile(picBox.ImageLocation);
                if (image.Height <= height && image.Width <= width)
                {
                    picBox.SizeMode = PictureBoxSizeMode.CenterImage;
                    picBox.Dock = DockStyle.Fill;
                }
                else
                {
                    picBox.SizeMode = PictureBoxSizeMode.AutoSize;
                    picBox.Dock = DockStyle.None;
                    picBox.Location = new Point(1, 0);
                    //MessageBox.Show("v--"+vmax+"-"+x+" h-- "+hmax+"-"+y);
                    pan_picture.AutoScrollPosition = new Point((vmax - x) / 2, (hmax - y) / 2);//设置滚动条位置为中间位置
                }
            }
            catch (Exception)
            {

            }

        }

        /// <summary>
        /// 设置为不停靠父容器,PictureBox大小等同于它所包含图像的大小
        /// </summary>
        /// <param name="picBox"></param>
        public static void resetPic(PictureBox picBox)
        {
            picBox.Dock = DockStyle.None;
            picBox.SizeMode = PictureBoxSizeMode.AutoSize;
        }

        /// <summary>
        /// 放大
        /// </summary>
        /// <param name="picBox"></param>
        public static void max(PictureBox picBox)
        {
            int w = picBox.Image.Width;
            int h = picBox.Image.Height;
            double div = Convert.ToDouble(h) / Convert.ToDouble(w);

            w = w + 30;
            h = Convert.ToInt32(w * div);

            picBox.Left -= 15;
            picBox.Top -= (h - picBox.Image.Height) / 2;
            Bitmap NewBitmap = new Bitmap(picBox.Image, w, h);
            picBox.Image = NewBitmap;
            NewBitmap.Dispose();
            //zoom_image(true,picBox,w,h);
        }

        /// <summary>
        /// 缩小
        /// </summary>
        /// <param name="picBox"></param>
        public static void min(PictureBox picBox)
        {
            int w = picBox.Image.Width;
            int h = picBox.Image.Height;
            double div = Convert.ToDouble(h) / Convert.ToDouble(w);

            if (w > 30 && (w - 30) * div > 1)
            {
                w = w - 30;
                h = Convert.ToInt32(w * div);
                picBox.Left += 15;
                picBox.Top -= (h - picBox.Image.Height) / 2;
            }
            Bitmap NewBitmap = new Bitmap(picBox.Image, w, h);
            picBox.Image = NewBitmap;
            NewBitmap.Dispose();
            //zoom_image(true, picBox, w, h);
        }


        /// <summary>
        /// 放大缩小
        /// </summary>
        /// <param name="picBox">PictureBox控件</param>
        /// <param name="img">原始图片</param>
        /// <param name="zoomtime">放大缩小次数,负为缩小,正为放大</param>
        /// <returns></returns>
        public static void maxMin(PictureBox picBox, Image img, int zoomtime)
        {
            int w = img.Width;
            int h = img.Height;
            Bitmap NewBitmap = new Bitmap(img);
            try
            {
                double div = Convert.ToDouble(h) / Convert.ToDouble(w);

                if (w > 30 && (w - 30) * div > 1)
                {
                    w = w + 30 * zoomtime;
                    h = Convert.ToInt32(w * div);

                    NewBitmap = new Bitmap(img, w, h);

                }

                //if (w < img.Width && h < img.Height)
                //{
                //    picBox.Location =new Point(0,0);
                // }

                picBox.Image = NewBitmap;
                //NewBitmap.Dispose();
                GC.Collect();

            }
            catch { }

        }


        /// <summary>
        /// 小于容器大小就不缩放
        /// </summary>
        /// <param name="picBox"></param>
        /// <param name="pan_picture"></param>
        /// <returns></returns>
        public static bool IsMaxMin(PictureBox picBox, Panel pan_picture)
        {
            bool yn = true;

            int w = picBox.Image.Width;
            int h = picBox.Image.Height;

            if (w < pan_picture.Width && h < pan_picture.Height)
            {
                SetSuitPictureBox(picBox, pan_picture);
                yn = false;
            }
            else
            {
                resetPic(picBox);
                yn = true;
            }

            return yn;
        }

        /// <summary>
        /// 绘制新Img
        /// </summary>
        /// <param name="chec"></param>
        /// <param name="pictureBox1"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        private static void zoom_image(bool chec, PictureBox pictureBox1, int w, int h)
        {
            int begin_x = pictureBox1.Location.X;        //图片开始位置x
            int begin_y = pictureBox1.Location.Y;        //图片开始位置y

            Image image_ori = pictureBox1.Image;

            //int w, h;                      //缩放后的图片大小            
            if (chec)
            {
                if (begin_x + pictureBox1.Width > w) begin_x = w - pictureBox1.Width;
                if (begin_y + pictureBox1.Height > h) begin_y = h - pictureBox1.Height;
                if (begin_x < 0) begin_x = 0;
                if (begin_y < 0) begin_y = 0;
            }
            Bitmap resizedBmp = new Bitmap(w, h);
            Graphics g = Graphics.FromImage(resizedBmp);
            //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
            //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
            g.DrawImage(image_ori, new Rectangle(0, 0, w, h), new Rectangle(0, 0, image_ori.Width, image_ori.Height), GraphicsUnit.Pixel);
            int ww, hh;
            ww = w;
            hh = h;
            if (pictureBox1.Width < ww) ww = pictureBox1.Width;
            if (pictureBox1.Height < hh) hh = pictureBox1.Height;
            try
            {
                pictureBox1.Image = resizedBmp;   //在图片框上显示区域图片
            }
            catch
            {
            }
            g.Dispose();
        }
    }
}

  

WinForm图片的移动缩放

上一篇:flex-direction


下一篇:asp.net core 读取appsettings.json