C# 实现QQ式截图功能实例代码
程序员文章站
2022-05-31 16:53:20
这个功能一共有两部分组成,第一部分是窗体代码,另外的一部分是一个辅助方法。直接贴出代码,以供大家参考:
using system;
using system...
这个功能一共有两部分组成,第一部分是窗体代码,另外的一部分是一个辅助方法。直接贴出代码,以供大家参考:
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 } }
实现的效果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。