窗体界面部分如下:

鼠标的缩放功能需要手动在 OpertaionImg.Designer.cs 文件里面添加一句代码,具体代码如下:

//picturePhoto显示图片的控件

this.picturePhoto.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.picturePhoto_MouseWheel);

下面是窗体的后台代码:

 public partial class OpertaionImg : Form
{
Bitmap myBmp;
Point mouseDownPoint = new Point(); //记录拖拽过程鼠标位置
bool isMove = false; //判断鼠标在picturebox上移动时,是否处于拖拽过程(鼠标左键是否按下)
int zoomStep = ; //缩放步长
public OpertaionImg()
{
InitializeComponent();
} private void btnChoise_Click(object sender, EventArgs e)
{
string filename = "";
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Tiff文件|*.tif|Bmp文件|*.bmp|Erdas img文件|*.img|EVNI文件|*.hdr|jpeg文件|*.jpg|raw文件|*.raw|vrt文件|*.vrt|所有文件|*.*";
dlg.FilterIndex = ;
if (dlg.ShowDialog() == DialogResult.OK)
{
filename = dlg.FileName;
}
if (filename == "")
{
return;
}
myBmp = new Bitmap(filename);
if (myBmp == null)
{
MessageBox.Show("读取失败");
return;
}
// textBox1.Text = filename;
picturePhoto.Image = myBmp;
picturePhoto.SizeMode = PictureBoxSizeMode.Zoom; //设置picturebox为缩放模式
picturePhoto.Width = myBmp.Width;
picturePhoto.Height = myBmp.Height;
} private void picturePhoto_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
isMove = true;
picturePhoto.Focus();
}
} private void picturePhoto_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMove = false;
}
} private void picturePhoto_MouseMove(object sender, MouseEventArgs e)
{
picturePhoto.Focus();
if (isMove)
{
int x, y;
int moveX, moveY;
moveX = Cursor.Position.X - mouseDownPoint.X;
moveY = Cursor.Position.Y - mouseDownPoint.Y;
x = picturePhoto.Location.X + moveX;
y = picturePhoto.Location.Y + moveY;
picturePhoto.Location = new Point(x, y);
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
}
}
private void picturePhoto_MouseWheel(object sender, MouseEventArgs e)
{
int x = e.Location.X;
int y = e.Location.Y;
int ow = picturePhoto.Width;
int oh = picturePhoto.Height;
int VX, VY;
if (e.Delta > )
{
picturePhoto.Width += zoomStep;
picturePhoto.Height += zoomStep; PropertyInfo pInfo = picturePhoto.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |BindingFlags.NonPublic);
Rectangle rect = (Rectangle)pInfo.GetValue(picturePhoto, null); picturePhoto.Width = rect.Width;
picturePhoto.Height = rect.Height;
}
if (e.Delta < )
{
if (picturePhoto.Width < myBmp.Width / )
return;
picturePhoto.Width -= zoomStep;
picturePhoto.Height -= zoomStep;
PropertyInfo pInfo = picturePhoto.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |BindingFlags.NonPublic);
Rectangle rect = (Rectangle)pInfo.GetValue(picturePhoto, null);
picturePhoto.Width = rect.Width;
picturePhoto.Height = rect.Height;
} VX = (int)((double)x * (ow - picturePhoto.Width) / ow);
VY = (int)((double)y * (oh - picturePhoto.Height) / oh);
picturePhoto.Location = new Point(picturePhoto.Location.X + VX, picturePhoto.Location.Y + VY);
} private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog sa = new SaveFileDialog();
//设置文件类型
sa.Filter = "图片文件(*.png)|*.png";
//设置默认文件类型显示顺序
sa.FilterIndex = ;
if (sa.ShowDialog() == DialogResult.OK)
{
Bitmap bit = new Bitmap(picturePhoto.Width, picturePhoto.Height);//实例化一个和窗体一样大的bitmap
Graphics g = Graphics.FromImage(bit);
g.CompositingQuality = CompositingQuality.HighQuality;//质量设为最高
// g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(panel2.Width, panel2.Height));//保存整个窗体为图片
g.CopyFromScreen(picturePhoto.PointToScreen(Point.Empty), Point.Empty, picturePhoto.Size);//只保存某个控件(这里是panel游戏区)
bit.Save(sa.FileName);//默认保存格式为PNG,保存成jpg格式质量不是很好
}
}
}

最新文章

  1. 线程池 Threadpool (还需要补充)
  2. Swift 3.0 【Swift 3.0 相较于 Swift 2.2 的变化】
  3. SQL、Linq、lamda表达式 同一功能不同写法
  4. php上传大文件设置方法
  5. EasyUI中Dialog的使用
  6. java&amp;postgresql时区总结
  7. 图片(img标签)的onerror事件
  8. Rich控件一
  9. Vagrant 快速入门
  10. iOS开发网络篇—多线程断点下载
  11. Python文件处理之文件指针(四)
  12. d指针在Qt上的应用及实现(d指针能实现二进制兼容)
  13. Spring MVC 原理
  14. PHP-SESSION深入理解
  15. vue弹出框的封装
  16. Chrome神器Vimium快捷键学习记录
  17. android进入页面会定位到ListView问题解决方法
  18. [BZOJ3237]连通图
  19. CF869C The Intriguing Obsession(组合数学瞎搞,O(n)莫名过)
  20. ELK_Elastic Search和kibana版本对应关系

热门文章

  1. BZOJ 1316: 树上的询问
  2. 2017 Multi-University Training Contest - Team 4 hdu6070 Dirt Ratio
  3. yarn 与 resource manager ha
  4. C#使用window API 控制打印纸张大小(转载)
  5. Java-线程池专题(什么是线程池,如何使用,为什么要用)
  6. netty7---自定义序列化接口
  7. 企业级hbase HA配置
  8. spring-boot单元测试
  9. Swoole学习(一)了解一下Swoole的强大并在Centos安装Swoole及PHP扩展开启
  10. JAVA基础补漏--SET