当前位置: 移动技术网 > IT编程>开发语言>.net > C#开发自动照片(图片)裁剪(缩放)工具

C#开发自动照片(图片)裁剪(缩放)工具

2019年09月17日  | 移动技术网IT编程  | 我要评论

流行服饰搭配,神捕铁中英,夸格米拉

1、需求分析

用winform窗体程序,开发一个能够自动、批量对图片进行缩放和裁剪的程序。

原本想直接从网上找类型的工具直接用,但是无奈现在网上能找到的工具,要么不能用,要么就是很

恶心的下载完后还有一堆插件的,要么就是我用不来的。

这个程序是我根据个人需求,想要把所有拍好的人像照,直接批处理成1寸的照片,就能省去很多裁剪图片的时间。

这里要对生成的1寸图片做个说明:1寸图片我网上查到的大小是:照片规格(英寸) (厘米) (像素)

  1寸 2.5*3.5cm 413*295

由于在拍摄时可能存在横拍和竖拍,所以生成后图片虽然也是1寸的,但是方向不一样,这个不影响,还可以根据需求在代码中修改。

先看效果:

 

 

 

 

 

 

 可以看出缩放图片后,图片的质量也会随之变低,而裁剪则不会有这个问题。

2、图片裁剪代码

 1      /// <summary>
 2         /// 裁剪按钮
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void button1_click(object sender, eventargs e)
 7         {
 8             openfiledialog dialog = new openfiledialog();                                         //打开文件夹,选择图片
 9             dialog.filter = "(*.jpg,*.png,*.jpeg,*.bmp,*.gif)|*.jgp;*.png;*.jpeg;*.bmp;*.gif;*.jpg;*.png;|all files(*.*)|*.*";
10             dialog.multiselect = true;
11             if (dialog.showdialog() == dialogresult.ok)
12             {
13                 string path = dialog.filename;
14                 bitmap img = new bitmap(path);
15                 image img2 = imagetailor(img, 319, 449);                                         //设置大小为1寸照片   
16                 string imgsaveurl = @"d:/我的图片/处理完成/" + dialog.safefilename;                //保存图片到文件夹
17                 img2.save(imgsaveurl);
18                 img2.dispose();                                                                  //释放资源
19             }
20         }
21 
22         public image imagetailor(bitmap bmp,int nowwidth,int nowheight)
23         {
24             bitmap newbm = new bitmap(nowwidth, nowheight);
25             graphics g = graphics.fromimage(newbm);
26             g.interpolationmode = interpolationmode.highqualitybicubic;
27             g.smoothingmode = smoothingmode.highquality;
28             g.compositingquality = compositingquality.highquality;
29             //前rectangle代表画布大小,后rectangle代表裁剪后右边留下的区域
30             g.drawimage(bmp, new rectangle(0, 0, nowwidth, nowheight), new rectangle(0, 0, bmp.width / 2, bmp.height), graphicsunit.pixel);
31             g.dispose();
32             return newbm;
33         }

 裁剪按钮方法还没完善好,目前只完成了单张图片的裁剪。要想修改成批量的,只需在openfiledialog控件设置成允许多选,然后根据选中的图片数量进行循环处理,就可以了。

后面的缩放按钮是比较完善的,可以允许多选图片,并进行批处理。

3、图片缩放代码:

缩放图片的代码看起来会比较复杂些,但是逻辑并不难。一些代码也是在网上看其他人写的,拿过来用(那个“中间件确实是自己写着玩的,懒得改回去,哈哈”)

  1      /// <summary>
  2         /// 显示图片
  3         /// </summary>
  4         /// <param name="sender"></param>
  5         /// <param name="e"></param>
  6         private void button_selectimg_click(object sender, eventargs e)
  7         {
  8             openfiledialog dialog = new openfiledialog();
  9             dialog.filter = "(*.jpg,*.png,*.jpeg,*.bmp,*.gif)|*.jgp;*.png;*.jpeg;*.bmp;*.gif;*.jpg;*.png;|all files(*.*)|*.*";
 10             dialog.multiselect = true;                                                              //允许文件多选
 11             string folder = "";
 12             if (directory.exists(@"d:/我的图片/处理完成/"))
 13             {
 14                 folder = @"d:/我的图片/处理完成/";
 15             }
 16             else
 17             {
 18                 directory.createdirectory(@"d:/我的图片/处理完成/");
 19                 folder = @"d:/我的图片/处理完成/"; 
 20             }
 21             if (dialog.showdialog() == dialogresult.ok) 
 22             {
 23                 int filecount = dialog.filenames.length;                                            //获取选中图片数量
 24                 int countnum = filecount;
 25                 for (int i = 0; i < filecount; i++)
 26                 {
 27                     string path = dialog.filenames[i];                                              //图片绝对路径,包含图片名称
 28                     image img = getreducedimage(path);
 29                     string imgsaveurl = folder + dialog.safefilenames[i];                           //保存图片到文件夹
 30                     img.save(imgsaveurl);
 31                     img.dispose();                                                                  //释放资源
 32                     countnum--;
 33                     if (countnum == 0)
 34                     {
 35                         messagebox.show("处理图片完成,总计处理:" + filecount + "张");
 36                     }
 37                 }
 38             }
 39         }
 40 
 41         /// <summary>
 42         /// 中间件,写着玩
 43         /// </summary>
 44         /// <param name="filename"></param>
 45         /// <returns></returns>
 46         public image getreducedimage(string filename)
 47         {
 48             image resourceimage = image.fromfile(filename);
 49             bitmap img = new bitmap(resourceimage);
 50             return zoomimage(img, 449, 319);
 51         }
 52 
 53         #region 等比例缩放图片
 54         /// <summary>
 55         /// 等比例缩放图片
 56         /// </summary>
 57         /// <param name="bitmap"></param>
 58         /// <param name="destheight"></param>
 59         /// <param name="destwidth"></param>
 60         /// <returns></returns>
 61         private bitmap zoomimage(bitmap bitmap, int destheight, int destwidth)
 62         {
 63             try
 64             {
 65                 image sourimage = bitmap;
 66                 imagecodecinfo ici = getencoder(imageformat.jpeg);
 67                 int width = 0, height = 0;
 68                 //按比例缩放           
 69                 int sourwidth = sourimage.width;
 70                 int sourheight = sourimage.height;
 71                 if (sourheight > destheight || sourwidth > destwidth)
 72                 {
 73                     if ((sourwidth * destheight) > (sourheight * destwidth))
 74                     {
 75                         width = destwidth;
 76                         height = (destwidth * sourheight) / sourwidth;
 77                     }
 78                     else
 79                     {
 80                         height = destheight;
 81                         width = (sourwidth * destheight) / sourheight;
 82                     }
 83                 }
 84                 else
 85                 {
 86                     width = sourwidth;
 87                     height = sourheight;
 88                 }
 89                 bitmap destbitmap = new bitmap(destwidth, destheight);
 90                 graphics g = graphics.fromimage(destbitmap);
 91                 g.clear(color.transparent);
 92                 //设置画布的描绘质量         
 93                 g.compositingquality = compositingquality.highquality;
 94                 g.smoothingmode = smoothingmode.highquality;
 95                 g.interpolationmode = interpolationmode.highqualitybilinear;
 96                 g.drawimage(sourimage, new rectangle((destwidth - width) / 2, (destheight - height) / 2, width, height), 0, 0, sourimage.width, sourimage.height, graphicsunit.pixel);
 97                 g.dispose();
 98                 //设置压缩质量     
 99                 encoderparameters encoderparams = new encoderparameters();
100                 long[] quality = new long[1];
101                 quality[0] = 100;
102                 encoderparameter encoderparam = new encoderparameter(system.drawing.imaging.encoder.quality, quality);
103                 encoderparams.param[0] = encoderparam;
104                 sourimage.dispose();
105                 return destbitmap;
106             }
107             catch
108             {
109                 return bitmap;
110             }
111         }
112         #endregion
113 
114         public static imagecodecinfo getencoder(imageformat format)
115         {
116             imagecodecinfo[] codecs = imagecodecinfo.getimagedecoders();
117             foreach (imagecodecinfo codec in codecs)
118             {
119                 if (codec.formatid == format.guid)
120                 {
121                     return codec;
122                 }
123             }
124             return null;
125         }

 

4、总结:

以上,就完成了简单的图片批量缩放(裁剪)成1寸图片的功能。

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网