比如滚轮向前推是缩小,向后推是放大,类似这样的。
是picturebox里图片的放大的缩小,不是控件放大和缩小
C#怎么用滚轮实现picturebox里的图片的放大和缩小
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
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
悬赏问题
- ¥15 下图接收小电路,谁知道原理
- ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
- ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
- ¥15 手机接入宽带网线,如何释放宽带全部速度
- ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
- ¥15 ETLCloud 处理json多层级问题
- ¥15 matlab中使用gurobi时报错
- ¥15 这个主板怎么能扩出一两个sata口
- ¥15 不是,这到底错哪儿了😭
- ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么