1 // 剪切 并保存
2 //在指定原图大小 以及截取的位置和大小
3 public void CutAndSaveImage(System.Drawing.Image originalImage,string dstpath, int cutX, int cutY, int cutWidth, int cutHeight, int drawX, int drawY, int drawWidth, int drawHeight, int PhotoWidth, int PhotoHeight)
4 {
5 //新建一个bmp图片
6 System.Drawing.Image bitmap = new System.Drawing.Bitmap(PhotoWidth, PhotoHeight);
7
8 //新建一个画板
9 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
10
11 //设置高质量插值法
12 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
13
14 //设置高质量,低速度呈现平滑程度
15 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
16
17 //清空画布并以透明背景色填充
18 g.Clear(System.Drawing.Color.White);
19
20 //在指定位置并且按指定大小绘制原图片的指定部分
21 g.DrawImage(originalImage, new System.Drawing.Rectangle((drawX <= 0 ? 0 : drawX), (drawY <= 0 ? 0 : drawY), drawWidth, drawHeight),
22 new System.Drawing.Rectangle((cutX < 0 ? 0 : cutX), (cutY<0 ? 0 : cutY), cutWidth, cutHeight), System.Drawing.GraphicsUnit.Pixel);
23
24 originalImage.Dispose();
25
26 if (File.Exists(dstpath))
27 File.Delete(dstpath);
28
29 if (dstpath.EndsWith("jpg"))
30 bitmap.Save(dstpath, System.Drawing.Imaging.ImageFormat.Jpeg);
31 else if (dstpath.EndsWith("png"))
32 bitmap.Save(dstpath, System.Drawing.Imaging.ImageFormat.Png);
33 else
34 bitmap.Save(dstpath);
35
36 bitmap.Dispose();
37 g.Dispose();
38
39 }