C#图片处理之:图片缩放和剪裁
1string strFileNameOld = @"D:/png/fenge/129267877539521154.png";
2 string strFileNameNew = @"D:/png/fenge/"+DateTime.Now.ToFileTime().ToString()+".png ";
3 int iWidth, iHeight, iMagnify;
4 Color myColor;
5 int i, j, i0, j0, i1, j1;
6
7 iMagnify = 8;
8 Bitmap bmpTmp = (Bitmap)System.Drawing.Image.FromFile(strFileNameOld);
9 Bitmap bmpNew = new Bitmap(bmpTmp.Width * iMagnify, bmpTmp.Height * iMagnify);
10 Graphics g = Graphics.FromImage(bmpNew);
11
12 iWidth = bmpTmp.Width;
13 iHeight = bmpTmp.Height;
14
15 try
16 {
17 for (i = 0; i < iWidth; i++)
18 {
19 i0 = i * iMagnify;
20 for (j = 0; j < iHeight; j++)
21 {
22 j0 = j * iMagnify;
23 myColor = bmpTmp.GetPixel(i, j);
24 for (i1 = i0; i1 < i0 + iMagnify; i1++)
25 for (j1 = j0; j1 < j0 + iMagnify; j1++)
26 bmpNew.SetPixel(i1, j1, myColor);
27 }
28 }
29 }
30 catch
31 {
32 string errormsg;
33 errormsg = "hi ";
34 }
35
36
37 bmpTmp.Dispose();
38 g.Dispose();
39 bmpNew.Save(strFileNameNew, System.Drawing.Imaging.ImageFormat.Bmp);
40
41[c-sharp] view plain copy
42
43 应听众点播要求,今天说说用C#做图片的缩放和剪裁,相信很多人会对这部分内容感兴趣,毕竟这个操作太实用了。
44
45 其实在GDI+中,缩放和剪裁可以看作同一个操作,无非就是原始区域的选择不同罢了。空口无凭,先看具体算法可能更好理解。
46
47 /// <summary>
48 /// Resize图片
49 /// </summary>
50 /// <param name="bmp">原始Bitmap</param>
51 /// <param name="newW">新的宽度</param>
52 /// <param name="newH">新的高度</param>
53 /// <param name="Mode">保留着,暂时未用</param>
54 /// <returns>处理以后的图片</returns>
55 public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH, int Mode)
56 {
57 try
58 {
59 Bitmap b = new Bitmap(newW, newH);
60 Graphics g = Graphics.FromImage(b);
61
62 // 插值算法的质量
63 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
64
65 g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
66 g.Dispose();
67
68 return b;
69 }
70 catch
71 {
72 return null;
73 }
74 }
75
76 // ===============================
77
78 /// <summary>
79 /// 剪裁 -- 用GDI+
80 /// </summary>
81 /// <param name="b">原始Bitmap</param>
82 /// <param name="StartX">开始坐标X</param>
83 /// <param name="StartY">开始坐标Y</param>
84 /// <param name="iWidth">宽度</param>
85 /// <param name="iHeight">高度</param>
86 /// <returns>剪裁后的Bitmap</returns>
87 public static Bitmap KiCut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
88 {
89 if (b == null)
90 {
91 return null;
92 }
93
94 int w = b.Width;
95 int h = b.Height;
96
97 if (StartX >= w || StartY >= h)
98 {
99 return null;
100 }
101
102 if (StartX + iWidth > w)
103 {
104 iWidth = w - StartX;
105 }
106
107 if (StartY + iHeight > h)
108 {
109 iHeight = h - StartY;
110 }
111
112 try
113 {
114 Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
115
116 Graphics g = Graphics.FromImage(bmpOut);
117 g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
118 g.Dispose();
119
120 return bmpOut;
121 }
122 catch
123 {
124 return null;
125 }
126 }
注意到区别了吗?提示,g.DrawImage中第二个new Rectangle。
目标其实都是new Rectangle(0, 0, iWidth, iHeight),缩放算法把整个原始图都往目标区域里塞new Rectangle(0, 0, bmp.Width, bmp.Height),而剪裁只是把原始区域上等宽等高的那个区域new Rectangle(StartX, StartY, iWidth, iHeight)1:1的塞到目标区域里。很容易吧。