C# 实现QQ式截图功能实例代码
更新时间:2020年6月25日 11:21 点击:2012
这个功能一共有两部分组成,第一部分是窗体代码,另外的一部分是一个辅助方法。直接贴出代码,以供大家参考:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using ImageClassLib; namespace ImageShear { public partial class Demo: Form { public Demo() { InitializeComponent(); } #region 点击打开图像 public string strHeadImagePath; //打开图片的路径 Bitmap Bi; //定义位图对像 private void button1_Click(object sender, EventArgs e) { openFileDialog1.Filter = "*.gif|*.jpg|*.JPEG|*.JPEG|*.bmp|*.bmp"; //设置读取图片类型 if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { strHeadImagePath = openFileDialog1.FileName; //this.Show(strHeadImagePath); Bi = new Bitmap(strHeadImagePath); //使用打开的图片路径创建位图对像 ImageCut1 IC = new ImageCut1(40, 112, this.pictureBox1.Width, this.pictureBox1.Height); //实例化ImageCut类,四个参数据分别表示为:x、y、width、heigth,(40、112)表示pictureBox1的Lcation的坐标,(120、144)表示pictureBox1控件的宽度和高度 this.pictureBox1.Image = IC.KiCut1((Bitmap)(this.GetSelectImage(this.pictureBox1.Width, this.pictureBox1.Height))); //(120、144)表示pictureBox1控件的宽度和高度 //this.pictureBox1.Image = (this.GetSelectImage(120, 144)); } catch (Exception ex) { MessageBox.Show("格式不对"); ex.ToString(); } } } #endregion #region 定义显示图像方法,即将打开的图像在pictureBox1控件显示 public void Show(string strHeadImagePath) { this.pictureBox1.Load(@strHeadImagePath); // } #endregion #region 获取图像 /// <summary> /// 获取指定宽度和高度的图像即使图片和pictureBox1控件一样宽和高,返回值为图片Image /// </summary> /// <param name="Width表示宽"></param> /// <param name="Height表示高"></param> /// <returns></returns> public Image GetSelectImage(int Width, int Height) { //Image initImage = this.pictureBox1.Image; Image initImage = Bi; //原图宽高均小于模版,不作处理,直接保存 if (initImage.Width <= Width && initImage.Height <= Height) { //initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg); return initImage; } else { //原始图片的宽、高 int initWidth = initImage.Width; int initHeight = initImage.Height; //非正方型先裁剪为正方型 if (initWidth != initHeight) { //截图对象 System.Drawing.Image pickedImage = null; System.Drawing.Graphics pickedG = null; //宽大于高的横图 if (initWidth > initHeight) { //对象实例化 pickedImage = new System.Drawing.Bitmap(initHeight, initHeight); pickedG = System.Drawing.Graphics.FromImage(pickedImage); //设置质量 pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //定位 Rectangle fromR = new Rectangle((initWidth - initHeight) / 2, 0, initHeight, initHeight); Rectangle toR = new Rectangle(0, 0, initHeight, initHeight); //画图 pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel); //重置宽 initWidth = initHeight; } //高大于宽的竖图 else { //对象实例化 pickedImage = new System.Drawing.Bitmap(initWidth, initWidth); pickedG = System.Drawing.Graphics.FromImage(pickedImage); //设置质量 pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //定位 Rectangle fromR = new Rectangle(0, (initHeight - initWidth) / 2, initWidth, initWidth); Rectangle toR = new Rectangle(0, 0, initWidth, initWidth); //画图 pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel); //重置高 initHeight = initWidth; } initImage = (System.Drawing.Image)pickedImage.Clone(); // //释放截图资源 pickedG.Dispose(); pickedImage.Dispose(); } return initImage; } } #endregion #region 点击button2按钮事件 private void button2_Click(object sender, EventArgs e) { this.label1.Text = "裁剪后的图片宽度:"+this.pictureBox2.Width.ToString(); this.label2.Text = "裁剪后的图片高度:"+this.pictureBox2.Height.ToString(); } #endregion #region 缩放、裁剪图像用到的变量 /// <summary> /// /// </summary> int x1; //鼠标按下时横坐标 int y1; //鼠标按下时纵坐标 int width; //所打开的图像的宽 int heigth; //所打开的图像的高 bool HeadImageBool = false; // 此布尔变量用来判断pictureBox1控件是否有图片 #endregion #region 画矩形使用到的变量 Point p1; //定义鼠标按下时的坐标点 Point p2; //定义移动鼠标时的坐标点 Point p3; //定义松开鼠标时的坐标点 #endregion #region 鼠标按下时发生的事件 private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { this.Cursor = Cursors.Cross; this.p1 = new Point(e.X, e.Y); x1 = e.X; y1 = e.Y; if (this.pictureBox1.Image != null) { HeadImageBool = true; } else { HeadImageBool = false; } } #endregion #region 移动鼠标发生的事件 private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (this.Cursor == Cursors.Cross) { this.p2 = new Point(e.X, e.Y); if ((p2.X - p1.X) > 0 && (p2.Y - p1.Y) > 0) //当鼠标从左上角向开始移动时P3坐标 { this.p3 = new Point(p1.X, p1.Y); } if ((p2.X - p1.X) < 0 && (p2.Y - p1.Y) > 0) //当鼠标从右上角向左下方向开始移动时P3坐标 { this.p3 = new Point(p2.X, p1.Y); } if ((p2.X - p1.X) > 0 && (p2.Y - p1.Y) < 0) //当鼠标从左下角向上开始移动时P3坐标 { this.p3 = new Point(p1.X, p2.Y); } if ((p2.X - p1.X) < 0 && (p2.Y - p1.Y) < 0) //当鼠标从右下角向左方向上开始移动时P3坐标 { this.p3 = new Point(p2.X, p2.Y); } this.pictureBox1.Invalidate(); //使控件的整个图面无效,并导致重绘控件 } } #endregion #region 松开鼠标发生的事件,实例化ImageCut1类对像 ImageCut1 IC1; //定义所画矩形的图像对像 private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { if (HeadImageBool) { width = this.pictureBox1.Image.Width; heigth = this.pictureBox1.Image.Height; if ((e.X - x1) > 0 && (e.Y - y1) > 0) //当鼠标从左上角向右下方向开始移动时发生 { IC1 = new ImageCut1(x1, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类 } if ((e.X - x1) < 0 && (e.Y - y1) > 0) //当鼠标从右上角向左下方向开始移动时发生 { IC1 = new ImageCut1(e.X, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类 } if ((e.X - x1) > 0 && (e.Y - y1) < 0) //当鼠标从左下角向右上方向开始移动时发生 { IC1 = new ImageCut1(x1, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类 } if ((e.X - x1) < 0 && (e.Y - y1) < 0) //当鼠标从右下角向左上方向开始移动时发生 { IC1 = new ImageCut1(e.X, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类 } this.pictureBox2.Width = (IC1.KiCut1((Bitmap)(this.pictureBox1.Image))).Width; this.pictureBox2.Height = (IC1.KiCut1((Bitmap)(this.pictureBox1.Image))).Height; this.pictureBox2.Image = IC1.KiCut1((Bitmap)(this.pictureBox1.Image)); this.Cursor = Cursors.Default; } else { this.Cursor = Cursors.Default; } } #endregion #region 获取所选矩形图像 /// <summary> /// /// </summary> /// <param name="Width"></param> /// <param name="Height"></param> /// <returns></returns> public Image GetSelectImage1(int Width, int Height) { Image initImage = this.pictureBox1.Image; //Image initImage = Bi; //原图宽高均小于模版,不作处理,直接保存 if (initImage.Width <= Width && initImage.Height <= Height) { //initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg); return initImage; } else { //原始图片的宽、高 int initWidth = initImage.Width; int initHeight = initImage.Height; //非正方型先裁剪为正方型 if (initWidth != initHeight) { //截图对象 System.Drawing.Image pickedImage = null; System.Drawing.Graphics pickedG = null; //宽大于高的横图 if (initWidth > initHeight) { //对象实例化 pickedImage = new System.Drawing.Bitmap(initHeight, initHeight); pickedG = System.Drawing.Graphics.FromImage(pickedImage); //设置质量 pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //定位 Rectangle fromR = new Rectangle((initWidth - initHeight) / 2, 0, initHeight, initHeight); Rectangle toR = new Rectangle(0, 0, initHeight, initHeight); //画图 pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel); //重置宽 initWidth = initHeight; } //高大于宽的竖图 else { //对象实例化 pickedImage = new System.Drawing.Bitmap(initWidth, initWidth); pickedG = System.Drawing.Graphics.FromImage(pickedImage); //设置质量 pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //定位 Rectangle fromR = new Rectangle(0, (initHeight - initWidth) / 2, initWidth, initWidth); Rectangle toR = new Rectangle(0, 0, initWidth, initWidth); //画图 pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel); //重置高 initHeight = initWidth; } initImage = (System.Drawing.Image)pickedImage.Clone(); // //释放截图资源 pickedG.Dispose(); pickedImage.Dispose(); } return initImage; } } #endregion #region 重新绘制pictureBox1控件,即移动鼠标画矩形 private void pictureBox1_Paint(object sender, PaintEventArgs e) { if (HeadImageBool) { Pen p = new Pen(Color.Black, 1);//画笔 p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //Bitmap bitmap = new Bitmap(strHeadImagePath); Bitmap bitmap = Bi; Rectangle rect = new Rectangle(p3, new Size(System.Math.Abs(p2.X - p1.X), System.Math.Abs(p2.Y - p1.Y))); e.Graphics.DrawRectangle(p, rect); } else { } } #endregion } }
第二部分是辅助方法类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; namespace ImageClassLib { public class ImageCut1 { #region 剪裁图片方法 /// <summary> /// 剪裁 -- 用GDI+ /// </summary> /// <param name="b">原始Bitmap,即需要裁剪的图片</param> /// <param name="StartX">开始坐标X</param> /// <param name="StartY">开始坐标Y</param> /// <param name="iWidth">宽度</param> /// <param name="iHeight">高度</param> /// <returns>剪裁后的Bitmap</returns> public Bitmap KiCut1(Bitmap b) { if (b == null) { return null; } int w = b.Width; int h = b.Height; if (X >= w || Y >= h) { return null; } if (X + Width > w) { Width = w - X; } if (Y + Height > h) { Height = h - Y; } try { Bitmap bmpOut = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(bmpOut); // Create rectangle for displaying image. Rectangle destRect = new Rectangle(0, 0, Width, Height); //所画的矩形正确,它指定所绘制图像的位置和大小。 将图像进行缩放以适合该矩形。 // Create rectangle for source image. Rectangle srcRect = new Rectangle(X, Y, Width, Height); //srcRect参数指定要绘制的 image 对象的矩形部分。 将此部分进行缩放以适合 destRect 参数所指定的矩形。 g.DrawImage(b, destRect, srcRect, GraphicsUnit.Pixel); //resultG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, side, side), new System.Drawing.Rectangle(0, 0, initWidth, initHeight), System.Drawing.GraphicsUnit.Pixel); g.Dispose(); return bmpOut; } catch { return null; } } #endregion #region ImageCut1类的构造函数 public int X; public int Y; public int Width ; public int Height; /// <summary> /// ImageCut1类的构造函数,ImageCut1类用来获取鼠标在pictureBox1控件所画矩形内的图像 /// </summary> /// <param name="x表示鼠标在pictureBox1控件上按下时的横坐标"></param> /// <param name="y表示鼠标在pictureBox1控件上按下时的纵坐标"></param> /// <param name="width表示鼠标在pictureBox1控件上松开鼠标的宽度"></param> /// <param name="heigth表示鼠标在pictureBox1控件上松开鼠标的高度"></param> public ImageCut1(int x, int y, int width, int heigth) { X = x; Y = y; Width = width; Height = heigth; } #endregion } }
实现的效果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
上一篇: C#读取XML的三种实现方式
相关文章
- 我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
- 这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
- 这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
- 这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25- 这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
- 这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
- 这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
- 下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要介绍了C#中list用法,结合实例形式分析了C#中list排序、运算、转换等常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25