C#(winform)实现图片的无损放大缩小【点击鼠标滚动键放大缩小】

using System;//来源于www.uzhanbao.com
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace ImageLoader
{
    public partial class Form1 : Form
    {

        private Rectangle selectedArea;
        private Image loadedImage;
        private Color selectionColor;
        private int count = 10;

        public Form1()
        {
            InitializeComponent();
            this.MouseWheel +=new MouseEventHandler(picBox1_MouseWheel);
            picBox1.SizeMode = PictureBoxSizeMode.Zoom;
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (loadedImage != null)
                {
                    loadedImage.Dispose();
                }
                try
                {

                    loadedImage = Image.FromFile(openFileDialog1.FileName);

                    //Get a contrasting color for the image selection marker
                    using (Bitmap bmp = new Bitmap(loadedImage))
                    {
                        selectionColor = GetDominantColor(bmp, false);
                        selectionColor = CalculateOppositeColor(selectionColor);
                    }

                    ///tZoom.Value = 1;

                    //resizePictureArea();



                    //Map the area selected in the thumbail to the actual image size
                    Rectangle zoomArea = new Rectangle();
                    Rectangle l

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实现winform中的PictureBox控件的鼠标滚动放大缩小图片,需要进行以下步骤: 1. 在PictureBox控件上注册MouseWheel事件。 2. 在MouseWheel事件中获取滚轮滚动的增量值(e.Delta),并计算当前PictureBox的缩放比例。 3. 根据缩放比例使用Graphics类的DrawImage方法绘制缩放后的图片。 以下是示例代码: ```csharp private void pictureBox1_MouseWheel(object sender, MouseEventArgs e) { int delta = e.Delta; float zoomFactor = delta > 0 ? 1.1f : 0.9f; // 缩放因子 float oldZoom = pictureBox1.Image.Size.Width / (float)pictureBox1.ClientSize.Width; // 旧的缩放比例 float newZoom = oldZoom * zoomFactor; // 新的缩放比例 // 限制缩放范围,防止图片过小或过大 if (newZoom < 0.1f) newZoom = 0.1f; if (newZoom > 10.0f) newZoom = 10.0f; // 计算新的图片大小 int newWidth = (int)(pictureBox1.Image.Size.Width / oldZoom * newZoom); int newHeight = (int)(pictureBox1.Image.Size.Height / oldZoom * newZoom); // 创建缩放后的图片 Image newImage = new Bitmap(newWidth, newHeight); using (Graphics g = Graphics.FromImage(newImage)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(pictureBox1.Image, new Rectangle(0, 0, newWidth, newHeight), new Rectangle(0, 0, pictureBox1.Image.Width, pictureBox1.Image.Height), GraphicsUnit.Pixel); } // 更新PictureBox控件的图片和缩放比例 pictureBox1.Image = newImage; pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; } ``` 注意:上述代码中的pictureBox1.SizeMode属性设置为PictureBoxSizeMode.Zoom,表示按比例缩放图片以适应PictureBox控件的大小。如果设置为PictureBoxSizeMode.Normal,则需要手动调整PictureBox控件的大小以适应缩放后的图片

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值