C#切割指定区域图片操作
2015-12-02 14:30
507 查看
使用winform制作了一个切割图片的功能,切一些固定大小的图片,比如头像。
界面如图:
打开本地图片
拖动选择切割区域
保存区域图片完成切割
界面如图:
打开本地图片
OpenFileDialog opdialog = new OpenFileDialog(); opdialog.InitialDirectory = @"C:\"; opdialog.FilterIndex = 1; opdialog.Filter = "Image Files(*.BMP;*.JPG;*.PNG)|*.BMP;*.JPG;*.PNG"; if (opdialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) this.pb_photo_original.Image = Image.FromFile(opdialog.FileName);
拖动选择切割区域
if (e.Button != MouseButtons.Left) return; if (oldCutRect.Contains(e.Location)) { this.newCutRect.Offset(e.Location.X - this.m_LastMSPoint.X, e.Location.Y - this.m_LastMSPoint.Y); if (newCutRect.Location.X < 0) newCutRect.X = 0; if (newCutRect.Location.Y < 0) newCutRect.Y = 0; if (newCutRect.Location.X + 128 > 300) newCutRect.X = 300 - 128; if (newCutRect.Location.Y + 160 > 300) newCutRect.Y = 300 - 160; this.panel_shade.Invalidate(oldCutRect, false); this.panel_shade.Invalidate(newCutRect, false); this.m_LastMSPoint = e.Location; oldCutRect = newCutRect; }
保存区域图片完成切割
Bitmap bitmap = new Bitmap(128, 160); //创建作图区域 Graphics graphic = Graphics.FromImage(bitmap); Point p = this.pb_photo_original.PointToScreen(this.oldCutRect.Location); //截取原图相应区域写入作图区 //graphic.DrawImage(this.pb_photo_original.Image, new Rectangle(0, 0, 128, 160), oldCutRect, GraphicsUnit.Pixel); graphic.CopyFromScreen(p.X, p.Y, 0, 0, new Size(128, 160)); Bitmap zoomBitmap = new Bitmap(bitmap, new Size(80, 100)); this.pb_photo_cut.Image = zoomBitmap; //保存图象 zoomBitmap.Save(@"D:\test.jpg"); bitmap.Dispose(); graphic.Dispose();
相关文章推荐
- C# Socket的实现
- C# 实现发送手机短信
- C# .NET编码规范
- .Net(C#)Parallel"循环"的解释以及与循环的比较
- 终于会用c#中的delegate(委托)
- c#知识点
- C#中利用pictureBox画点的心得
- pp看书笔记---C#高级编程第九版 第三章 【类和结构】
- C# 日期和时间的字符串表示形式转换为其等效的DateTime(stringToDateTime)
- .net c# 视频剪切抓取缩略图
- C#_Assembly-Reflection_程序集与反射 练习<二> 记事本插件编写
- C#中IDisposable 回收非托管资源
- C# 使用 NPOI 库读写 Excel 文件(转载)
- swig编译GDAL的C#库时遇到的代码安全问题及解决方法
- C#中按钮响应事件简单或者相似响应函数的书写。
- .NET框架
- 【C#】按钮长按
- c# 学习笔记
- C#获取窗口,模拟按键操作
- C#可删除对象(学习)