c#picturebox缩放
·
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
}
//pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
private float zoom = 1f; // 初始缩放比例
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
// 确定缩放步长,可以根据需要调整
float zoomDelta = e.Delta > 0 ? 0.1f : -0.1f;
// 更新缩放比例
zoom += zoomDelta;
// 限制缩放比例,防止过小或过大
if (zoom < 0.1f) zoom = 0.1f;
if (zoom > 5f) zoom = 5f;
// 应用缩放
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Width = (int)(pictureBox1.Image.Width * zoom);
pictureBox1.Height = (int)(pictureBox1.Image.Height * zoom);
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Bitmap bitmap = new Bitmap("C:\\Users\\Administrator\\Desktop\\test.png");
pictureBox1.Image = bitmap;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp5
{
public partial class Form1 : Form
{
private Image originalImage;
private float scale = 1.0f;
private float offsetX = 0;
private float offsetY = 0;
private const float MinScale = 0.1f;
private const float MaxScale = 5.0f;
private const float ZoomFactor = 0.1f;
public Form1()
{
InitializeComponent();
pictureBox1.MouseWheel += new MouseEventHandler(PictureBox1_MouseWheel);
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
pictureBox1.Image = null; // 不直接设置 Image,由 Paint 绘制
// 加载原始图像(替换成你的路径或资源)
// 订阅事件
pictureBox1.Paint += PictureBox1_Paint;
pictureBox1.MouseWheel += PictureBox1_MouseWheel;
pictureBox1.MouseDown += PictureBox1_MouseDown;
pictureBox1.MouseMove += PictureBox1_MouseMove;
// 可选:启用双缓冲减少闪烁
typeof(Control).InvokeMember("DoubleBuffered",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.SetProperty,
null, pictureBox1, new object[] { true });
}
//pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
if (originalImage == null) return;
// 计算绘制区域:缩放 + 平移
Rectangle destRect = new Rectangle(
(int)(offsetX),
(int)(offsetY),
(int)(originalImage.Width * scale),
(int)(originalImage.Height * scale));
// 使用高质量插值(可选)
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
e.Graphics.DrawImage(originalImage, destRect);
}
private void PictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
// 获取鼠标在 PictureBox 中的位置
float mousePosX = e.X;
float mousePosY = e.Y;
// 记录缩放前图像上对应点的世界坐标
float worldXBefore = (mousePosX - offsetX) / scale;
float worldYBefore = (mousePosY - offsetY) / scale;
// 计算新缩放比例
float delta = e.Delta > 0 ? ZoomFactor : -ZoomFactor;
scale += delta;
// 限制缩放范围
if (scale < MinScale) scale = MinScale;
if (scale > MaxScale) scale = MaxScale;
// 根据世界坐标不变原则,反推新的 offset
offsetX = mousePosX - worldXBefore * scale;
offsetY = mousePosY - worldYBefore * scale;
pictureBox1.Invalidate(); // 触发重绘
}
// ====== 可选:支持拖拽平移 ======
private Point lastMousePos;
private bool isDragging = false;
private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragging = true;
lastMousePos = e.Location;
pictureBox1.Cursor = Cursors.SizeAll;
}
}
private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
offsetX += e.X - lastMousePos.X;
offsetY += e.Y - lastMousePos.Y;
lastMousePos = e.Location;
pictureBox1.Invalidate();
}
}
private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragging = false;
pictureBox1.Cursor = Cursors.Default;
}
}
private void button1_Click(object sender, EventArgs e)
{
//using (OpenFileDialog openFileDialog = new OpenFileDialog())
//{
// openFileDialog.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp;*.gif";
// openFileDialog.RestoreDirectory = true;
// if (openFileDialog.ShowDialog() == DialogResult.OK)
// {
// // 加载用户选择的图片
// originalImage?.Dispose(); // 释放之前加载的图像资源
// originalImage = Image.FromFile(openFileDialog.FileName);
// // 重置缩放比例和偏移量
// scale = 1.0f;
// offsetX = 0;
// offsetY = 0;
// pictureBox1.Invalidate(); // 触发重绘
// }
//}
originalImage = Image.FromFile(@"C:\Users\Administrator\Desktop\test.png");
pictureBox1.Invalidate();
//Bitmap bitmap = new Bitmap("C:\\Users\\Administrator\\Desktop\\test.png");
//pictureBox1.Image = bitmap;
}
}
}
更多推荐



所有评论(0)