C# 实现QQ式截图功能实例代码

时间:2022-06-01 21:39:18

这个功能一共有两部分组成,第一部分是窗体代码,另外的一部分是一个辅助方法。直接贴出代码,以供大家参考:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
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
  }
}

第二部分是辅助方法类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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# 实现QQ式截图功能实例代码

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/felix-wang/p/6367314.html