最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
C# 实现QQ式截图功能实例代码
时间:2017-03-23 编辑:简简单单 来源:一聚教程网
这个功能一共有两部分组成,第一部分是窗体代码,另外的一部分是一个辅助方法。直接贴出代码,以供大家参考:
代码如下 | 复制代码 |
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.Drawing.Drawing2D; usingSystem.Drawing.Imaging; usingImageClassLib; namespaceImageShear { publicpartialclassDemo: Form { publicDemo() { InitializeComponent(); } #region 点击打开图像 publicstringstrHeadImagePath;//打开图片的路径 Bitmap Bi;//定义位图对像 privatevoidbutton1_Click(objectsender, EventArgs e) { openFileDialog1.Filter ="*.gif|*.jpg|*.JPEG|*.JPEG|*.bmp|*.bmp"; //设置读取图片类型 if(openFileDialog1.ShowDialog() == DialogResult.OK) { try { strHeadImagePath = openFileDialog1.FileName; //this.Show(strHeadImagePath); Bi =newBitmap(strHeadImagePath);//使用打开的图片路径创建位图对像 ImageCut1 IC =newImageCut1(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控件显示 publicvoidShow(stringstrHeadImagePath) { this.pictureBox1.Load(@strHeadImagePath); // } #endregion #region 获取图像 /// /// 获取指定宽度和高度的图像即使图片和pictureBox1控件一样宽和高,返回值为图片Image /// /// /// /// publicImage GetSelectImage(intWidth,intHeight) { //Image initImage = this.pictureBox1.Image; Image initImage = Bi; //原图宽高均小于模版,不作处理,直接保存 if(initImage.Width <= Width && initImage.Height <= Height) { //initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg); returninitImage; } else { //原始图片的宽、高 intinitWidth = initImage.Width; intinitHeight = initImage.Height;
//非正方型先裁剪为正方型 if(initWidth != initHeight) { //截图对象 System.Drawing.Image pickedImage =null; System.Drawing.Graphics pickedG =null;
//宽大于高的横图 if(initWidth > initHeight) { //对象实例化 pickedImage =newSystem.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 =newRectangle((initWidth - initHeight) / 2, 0, initHeight, initHeight); Rectangle toR =newRectangle(0, 0, initHeight, initHeight); //画图 pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel); //重置宽 initWidth = initHeight; } //高大于宽的竖图 else { //对象实例化 pickedImage =newSystem.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 =newRectangle(0, (initHeight - initWidth) / 2, initWidth, initWidth); Rectangle toR =newRectangle(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(); }
returninitImage; } } #endregion #region 点击button2按钮事件 privatevoidbutton2_Click(objectsender, EventArgs e) { this.label1.Text ="裁剪后的图片宽度:"+this.pictureBox2.Width.ToString(); this.label2.Text ="裁剪后的图片高度:"+this.pictureBox2.Height.ToString(); } #endregion #region 缩放、裁剪图像用到的变量 /// /// /// intx1; //鼠标按下时横坐标 inty1; //鼠标按下时纵坐标 intwidth;//所打开的图像的宽 intheigth;//所打开的图像的高 boolHeadImageBool =false; // 此布尔变量用来判断pictureBox1控件是否有图片 #endregion #region 画矩形使用到的变量 Point p1; //定义鼠标按下时的坐标点 Point p2; //定义移动鼠标时的坐标点 Point p3; //定义松开鼠标时的坐标点 #endregion #region 鼠标按下时发生的事件 privatevoidpictureBox1_MouseDown(objectsender, MouseEventArgs e) { this.Cursor = Cursors.Cross; this.p1 =newPoint(e.X, e.Y); x1 = e.X; y1 = e.Y; if(this.pictureBox1.Image !=null) { HeadImageBool =true; } else { HeadImageBool =false; } } #endregion #region 移动鼠标发生的事件 privatevoidpictureBox1_MouseMove(objectsender, MouseEventArgs e) { if(this.Cursor == Cursors.Cross) { this.p2 =newPoint(e.X, e.Y); if((p2.X - p1.X) > 0 && (p2.Y - p1.Y) > 0) //当鼠标从左上角向开始移动时P3坐标 { this.p3 =newPoint(p1.X, p1.Y); } if((p2.X - p1.X) < 0 && (p2.Y - p1.Y) > 0) //当鼠标从右上角向左下方向开始移动时P3坐标 { this.p3 =newPoint(p2.X, p1.Y); } if((p2.X - p1.X) > 0 && (p2.Y - p1.Y) < 0) //当鼠标从左下角向上开始移动时P3坐标 { this.p3 =newPoint(p1.X, p2.Y); } if((p2.X - p1.X) < 0 && (p2.Y - p1.Y) < 0) //当鼠标从右下角向左方向上开始移动时P3坐标 { this.p3 =newPoint(p2.X, p2.Y); } this.pictureBox1.Invalidate();//使控件的整个图面无效,并导致重绘控件 } } #endregion #region 松开鼠标发生的事件,实例化ImageCut1类对像 ImageCut1 IC1;//定义所画矩形的图像对像 privatevoidpictureBox1_MouseUp(objectsender, MouseEventArgs e) { if(HeadImageBool) { width =this.pictureBox1.Image.Width; heigth =this.pictureBox1.Image.Height; if((e.X - x1) > 0 && (e.Y - y1) > 0) //当鼠标从左上角向右下方向开始移动时发生 { IC1 =newImageCut1(x1, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类 } if((e.X - x1) < 0 && (e.Y - y1) > 0) //当鼠标从右上角向左下方向开始移动时发生 { IC1 =newImageCut1(e.X, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类 } if((e.X - x1) > 0 && (e.Y - y1) < 0) //当鼠标从左下角向右上方向开始移动时发生 { IC1 =newImageCut1(x1, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类 } if((e.X - x1) < 0 && (e.Y - y1) < 0) //当鼠标从右下角向左上方向开始移动时发生 { IC1 =newImageCut1(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 获取所选矩形图像 /// /// /// /// /// /// publicImage GetSelectImage1(intWidth,intHeight) { Image initImage =this.pictureBox1.Image; //Image initImage = Bi; //原图宽高均小于模版,不作处理,直接保存 if(initImage.Width <= Width && initImage.Height <= Height) { //initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg); returninitImage; } else { //原始图片的宽、高 intinitWidth = initImage.Width; intinitHeight = initImage.Height;
//非正方型先裁剪为正方型 if(initWidth != initHeight) { //截图对象 System.Drawing.Image pickedImage =null; System.Drawing.Graphics pickedG =null;
//宽大于高的横图 if(initWidth > initHeight) { //对象实例化 pickedImage =newSystem.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 =newRectangle((initWidth - initHeight) / 2, 0, initHeight, initHeight); Rectangle toR =newRectangle(0, 0, initHeight, initHeight); //画图 pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel); //重置宽 initWidth = initHeight; } //高大于宽的竖图 else { //对象实例化 pickedImage =newSystem.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 =newRectangle(0, (initHeight - initWidth) / 2, initWidth, initWidth); Rectangle toR =newRectangle(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(); }
returninitImage; } } #endregion #region 重新绘制pictureBox1控件,即移动鼠标画矩形 privatevoidpictureBox1_Paint(objectsender, PaintEventArgs e) { if(HeadImageBool) { Pen p =newPen(Color.Black, 1);//画笔 p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //Bitmap bitmap = new Bitmap(strHeadImagePath); Bitmap bitmap = Bi; Rectangle rect =newRectangle(p3,newSize(System.Math.Abs(p2.X - p1.X), System.Math.Abs(p2.Y - p1.Y))); e.Graphics.DrawRectangle(p, rect); } else {
} } #endregion } } |
第二部分是辅助方法类
代码如下 | 复制代码 |
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Drawing; usingSystem.Drawing.Drawing2D; usingSystem.Drawing.Imaging; namespaceImageClassLib { publicclassImageCut1 { #region 剪裁图片方法 /// /// 剪裁 -- 用GDI+ /// /// 原始Bitmap,即需要裁剪的图片 /// 开始坐标X /// 开始坐标Y /// 宽度 /// 高度 /// publicBitmap KiCut1(Bitmap b) { if(b ==null) { returnnull; }
intw = b.Width; inth = b.Height;
if(X >= w || Y >= h) { returnnull; }
if(X + Width > w) { Width = w - X; }
if(Y + Height > h) { Height = h - Y; }
try { Bitmap bmpOut =newBitmap(Width, Height, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpOut); // Create rectangle for displaying image. Rectangle destRect =newRectangle(0, 0, Width, Height); //所画的矩形正确,它指定所绘制图像的位置和大小。 将图像进行缩放以适合该矩形。
// Create rectangle for source image. Rectangle srcRect =newRectangle(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(); returnbmpOut; } catch { returnnull; } } #endregion #region ImageCut1类的构造函数 publicintX; publicintY; publicintWidth ; publicintHeight; /// /// ImageCut1类的构造函数,ImageCut1类用来获取鼠标在pictureBox1控件所画矩形内的图像 /// /// /// /// /// publicImageCut1(intx,inty,intwidth,intheigth) { X = x; Y = y; Width = width; Height = heigth; } #endregion } } |
实现的效果如下:
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10