m0_59286135 2021-12-21 13:21 采纳率: 87.5%
浏览 311
已结题

C#怎么用滚轮实现picturebox里的图片的放大和缩小

比如滚轮向前推是缩小,向后推是放大,类似这样的。
是picturebox里图片的放大的缩小,不是控件放大和缩小

  • 写回答

2条回答 默认 最新

  • Ival 2021-12-21 14:31
    关注

    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

        private Image sourceImage = null;
        private int imageWidth = 0;
        private int imageHeight = 0;
        private float zoom = 1;
        private const float reduce = 0.1f;
        private const float enlarge = 1f;
    
        private void Form1_Load(object sender, EventArgs e)
        {
            sourceImage = ZoomImage.Properties.Resources._123456;
            imageWidth = sourceImage.Width;
            imageHeight = sourceImage.Height;
            pictureBox1.MouseWheel += pictureBox1_MouseWheel;
        }
    
        private 
        void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                if (zoom > 1)
                {
                    zoom -= enlarge;
                }
                else
                {
                    zoom -= reduce;
                }
                if (zoom >= 10)
                {
                    zoom = 10;
                }
                if (zoom <= 0)
                {
                    zoom = 0.1f;
                }
            }
            else
            {
                if (zoom > 1)
                {
                    zoom += enlarge;
                }
                else
                {
                    zoom += reduce;
                }
                if (zoom >= 10)
                {
                    zoom = 10;
                }
                if (zoom <= 0)
                {
                    zoom = 0.1f;
                }
            }
    
            pictureBox1.Image = ZoomImageMethod((Image)ZoomImage.Properties.Resources._123456, Convert.ToInt32(Math.Ceiling(imageWidth * zoom)), Convert.ToInt32(Math.Ceiling(imageHeight * zoom)));
        }
    
        private Image ZoomImageMethod(Image bitmap, int destHeight, int destWidth)
        {
            try
            {
                System.Drawing.Image sourImage = bitmap;
                int width = 0, height = 0;
                //按比例缩放             
                int sourWidth = sourImage.Width;
                int sourHeight = sourImage.Height;
                if (sourHeight > destHeight || sourWidth > destWidth)
                {
                    if (sourHeight / (destHeight * 1.0) > sourWidth / (destWidth * 1.0))
                    {
                        height = destHeight;
                        width = Convert.ToInt32(Math.Floor(sourWidth * destHeight / (sourHeight * 1.0)));
                    }
                    else
                    {
                        width = destWidth;
                        height = Convert.ToInt32(Math.Floor(sourHeight * destWidth / (sourWidth * 1.0)));
                    }
                }
                else
                {
                    width = sourWidth;
                    height = sourHeight;
                }
                Bitmap destBitmap = new Bitmap(destWidth, destHeight);
                Graphics g = Graphics.FromImage(destBitmap);
                g.Clear(Color.White);
                //设置画布的描绘质量           
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(sourImage, new Rectangle((destWidth - width) / 2, (destHeight - height) / 2, width, height), 0, 0, sourImage.Width, sourImage.Height, GraphicsUnit.Pixel);
                g.Dispose();
                //设置压缩质量       
                System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
                long[] quality = new long[1];
                quality[0] = 100;
                System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                encoderParams.Param[0] = encoderParam;
                sourImage.Dispose();
                return destBitmap;
            }
            catch
            {
                return bitmap;
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月28日
  • 已采纳回答 9月20日
  • 创建了问题 12月21日

悬赏问题

  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启