c# - 缩放到 PictureBox 中的点
问题描述
我有在 Panel 内的 PictureBox。Panel 有设置属性 AutoScroll == true,PictureBox 有 Sizemode == Zoom。如何简单地实现缩放以指向此代码?
public partial class frmMain : Form
{
string imageFilename = @"c:\Temp\test.jpg";
private Image _currentImage;
private float _zoom = 1.0f;
private Point _currMousePos;
public frmMain()
{
InitializeComponent();
RefreshImage(imageFilename);
pictureBox.MouseWheel += PictureBox_MouseWheel;
}
private void RefreshImage(string fileName)
{
_currentImage = Image.FromFile(fileName);
pictureBox.Invalidate();
}
private void PictureBox_MouseWheel(object sender, MouseEventArgs e)
{
if (e.Delta > 0)
{
_zoom += 0.1F;
}
else if (e.Delta < 0)
{
_zoom = Math.Max(_zoom - 0.1F, 0.01F);
}
MouseEventArgs mouse = e as MouseEventArgs;
_currMousePos = mouse.Location;
pictureBox.Invalidate();
// how to calculate Panel.AutoScrollPosition?
}
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
((Bitmap)_currentImage).SetResolution(e.Graphics.DpiX, e.Graphics.DpiY);
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.ScaleTransform(_zoom, _zoom);
e.Graphics.DrawImage(_currentImage, 0, 0);
pictureBox.Size = new Size((int)(float)(_currentImage.Width * _zoom),
(int)(float)(_currentImage.Height * _zoom));
}
}
还是我应该选择一种完全不同的缩放方式?
解决方案
推荐阅读
- python - 是否有一个 python 包可以从我的数据中找到最有影响力的组(分类特征)?
- java - 为什么我们需要在创建 java web 时为放入服务器的类实现 Serializable
- swift - 如何快速将相同的随机数分配给 2 个变量?
- python - 如何将字典列表附加到python中的相同列表?
- swift - Swift 字符串标记器/解析器
- sql-server - 将数字转换为日期时间
- python - 获取列表列表中大于某个阈值的所有元素的列表列表
- python - AttributeError:“NoneType”对象没有属性“发送”不和谐机器人
- google-cloud-platform - GCloud 命令不适用于 SSH(gcloud app deploy)
- c++ - C ++从带有字符的文件中读取数字