窗体界面部分如下:

鼠标的缩放功能需要手动在 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格式质量不是很好
}
}
}

C# 图片缩放,拖拽后保存成图片的功能的更多相关文章

  1. jqueryui sortable拖拽后保存位置

    jqueryUI sortable 可以用来进行页面拖拽布局,然而有一个小问题就是拖拽后如何保存状态. 工作中遇到了这个情况,遍把这个问题记了下来,具体思路是: 利用拖拽stop后利用   var a ...

  2. twaver拓扑图拖拽后保存json数据

    功能描述:拓扑图.对节点进行拖拽,序列化获取拓扑图信息,保存到本地localStorage,刷新页面,执行反序列化,从本地获取之前保存的数据,展现之前拖拽后的拓扑 拓展:此处存储用的是web本地存储l ...

  3. BitmapUtil【缩放bitmap以及将bitmap保存成图片到SD卡中】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 用于缩放bitmap以及将bitmap保存成图片到SD卡中 效果图 代码分析 bitmapZoomByHeight(Bitmap s ...

  4. flutter实现可缩放可拖拽双击放大的图片功能

    flutter实现可缩放可拖拽双击放大的图片功能 可缩放可拖拽的功能,可实现图片或者其他widget的缩放已经拖拽并支持双击放大的功能 我们知道官方提供了双击缩放,但是不支持拖拽的功能,我们要实现向百 ...

  5. vue组件实现图片的拖拽和缩放

    vue实现一个组件其实很简单但是要写出一个好的可复用的组件那就需要多学习和钻研一下,一个好的组件必须有其必不可少的有优点:一是能提高应用开发效率.测试性.复用性等:二是组件应该是高内聚.低耦合的:三是 ...

  6. 如何实现批量截取整个网页完整长截图,批量将网页保存成图片web2pic/webshot/screencapture/html2picture

    如何实现批量截取整个网页完整长截图,批量将网页保存成图片web2pic/webshot/screencapture [困扰?疑问?]: 您是否正受到:如何将网页保存为图片的困扰?网页很高很长截图截不全 ...

  7. javacpp-opencv图像处理之1:实时视频添加文字水印并截取视频图像保存成图片,实现文字水印的字体、位置、大小、粗度、翻转、平滑等操作

    欢迎大家积极开心的加入讨论群 群号:371249677 (点击这里进群) javaCV图像处理系列: javaCV图像处理之1:实时视频添加文字水印并截取视频图像保存成图片,实现文字水印的字体.位置. ...

  8. OpenGL中的深度、深度缓存、深度测试及保存成图片

    1.深度 所谓深度,就是在openGL坐标系中,像素点Z坐标距离摄像机的距离.摄像机可能放在坐标系的任何位置,那么,就不能简单的说Z数值越大或越小,就是越靠近摄像机. 2.深度缓冲区 深度缓冲区原理就 ...

  9. Bootstrap 可视化布局--拖拽后弹窗进行编辑

    Bootstrap 可视化布局--拖拽后弹窗进行编辑 最近后台想一个需求,使用可视化布局-中文 | en中拖拽表格后,弹窗进行编辑,保存下载后在后台生成pdf格式. 奈何各种问题不断,使用 jquer ...

随机推荐

  1. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D. Innokenty and a Football League

    地址:http://codeforces.com/contest/782/problem/D 题目: D. Innokenty and a Football League time limit per ...

  2. iOS 使用node js 搭建简单的本地服务器

    一.前提:基于iOS 项目 调用,使用了第三方框架NodeMobile.技术说明关键是 应用生命整个周期只能在应用启动时候开辟的一个线程里申请 一个 node  js 资源.如果终止了运行,重启是不支 ...

  3. 20145301 《Java程序设计》第八周学习总结

    20145301 <Java程序设计>第八周学习总结 教材学习内容总结 第十五章部分 - 通用API 日志API 日志: 日志对信息安全意义重大,审计.取证.入侵检测等都会用到日志信息 L ...

  4. linux系统调用是通过软中断实现的吗

    软中断是利用硬件中断的概念,用软件方式进行模拟,实现宏观上的异步执行效果.很多情况下,软中断和信号有些类似,同时,软中断又是和硬中断相对应的,硬中断是外部设备对CPU的中断,软中断通常是硬中断服务程序 ...

  5. 开发人员不可不看的 OBD通讯协议知识

    OBD-II Network Standards» J1850 VPW– Adopted by GM; also known as Class 2.– Adopted by Chrysler (kno ...

  6. 再也不学AJAX了!(三)跨域获取资源 ② - JSONP & CORS

    浏览器的"同源策略"固然保障了互联网世界的数据隐私与数据安全,但是如果当我们需要使用AJAX跨域请求资源时,"同源策略"又会成为开发者的阻碍.在本文中,我们会简 ...

  7. MR案例:Map-Join

    适用场景:一张表十分小[key不可重复].一张表非常大. 用法:在Job提交时,首先将小表加载到 DistributedCache 分布式缓存中,然后从DistributeCache中读取小表解析成 ...

  8. mongodb 索引的创建

    mongodb 创建常用的创建索引的有 signle Field Indexes Compound multikey,创建索引就是按照索引字段把documnet 进行排序,索引只存储了document ...

  9. 4.9版本linux内核的ina220电流检测芯片源码在哪里

    答:在drivers/hwmon/ina2xx.c中,内核配置项为CONFIG_SENSORS_INA2XX Location: -> Device Drivers -> Hardware ...

  10. spring junit4 测试

    @Service @ContextConfiguration(locations = { "classpath:config/applicationContext.xml" }) ...