C # Development Auto Photo (Picture) Crop (Zoom) Tool

1. Demand analysis

Using the winform form program, develop a program that can automatically zoom and crop pictures in batches.

I originally wanted to find the type of tools from the Internet and use them directly, but the tools that can be found on the Internet are either unusable or very

disgustingly, there are still a bunch of tools after downloading. The plug-in, or I can’t use it.

This program is based on my personal needs. I want to batch process all the portrait photos taken directly into 1-inch photos, which can save a lot of time for cropping pictures.

Here is an explanation of the generated 1-inch picture: The size of the 1-inch picture I found online is: photo size (inch) (cm) (pixel)

  1 inch 2.5*3.5cm 413*295

Because there may be horizontal and vertical shooting during shooting, although the generated picture is also 1-inch, but the direction is different, this does not affect, you can also Modified in the code.

Look at the effect first:

share picture

Share pictures

Share pictures

It can be seen that the quality of the image will be lower after zooming the image, and cropping will not have this problem.

2. Image cropping code

 1      /// 

2 /// Crop button
3 ///

4 ///
5 ///
6 private void Button1_Click(object sender, EventArgs e)
7 {
8 OpenFileDialog dialog = new OpenFileDialog(); < span style="color: #008000;">//Open the folder and select the picture
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); //Settings 1 inch photo
16 string imgSaveUrl = @"D:/My picture/Processing completed/" + dialog.SafeFileName; //Save the picture to the folder
17 img2.Save(imgSaveUrl);
18 img2.Dispose(); //< span style="color: #008000;">Release resources
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 //The front Rectangle represents the size of the canvas, and the back Rectangle represents the area left on the right after cropping
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 }

The method of cutting the button is not perfect yet, so far, only the single The crop of a picture. If you want to modify it into batches, you only need to set the openfiledialog control to allow multiple selections, and then perform loop processing according to the number of selected pictures.

The zoom button at the back is more complete, allowing multiple selection of pictures and batch processing.

3. Picture zoom code:

The code for zooming pictures looks more complicated, but the logic is not difficult. Some codes are also written by other people on the Internet, and used them (the “middleware is indeed written by myself, I don’t want to change it back, haha”)

< span style="color: #008080;"> 1      ///  

2 /// Show picture
3 ///

4 ///
5 ///
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; //Allow multiple selection of files
11 string folder = "";
12 if (Directory.Exists(@"D:/My picture/Processing completed/"))
13 {
14 folder = @"D:/My picture/Processing completed/" ;
15 }
16 else
17 {
18 Directory.CreateDirectory(@"D:/My picture/Processing completed/");
19 folder = @"D:/My picture/Processing completed/" ;
20 }
21 if (dialog.ShowDialog() == DialogResult.OK)
22 {
23 int fileCount = dialog.FileNames.Length; //Get the number of selected pictures
24 int countNum = fileCount;
25 for (int i = 0; i )
26 {
27 string path = dialog.FileNames[i] ; //The absolute path of the picture, including the picture name
28 Image img = GetReducedImage(path);
29 string imgSaveUrl = folder + dialog.SafeFileNames[ i]; //Save the picture to a folder
30 img.Save(imgSaveUrl);
31 img.Dispose(); //< span style="color: #008000;">Release resources
32 countNum--;
33 if (countNum == 0)
34 {
35 MessageBox.Show("Processing of pictures is complete, total processing:" + fileCount + "sheets");
36 }
37 }
38 }
39 }
40
41 ///
42 /// Middleware, write and play
43 ///

44 ///
45 ///
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 Scaling the image proportionally
54 ///
55 /// Scaling pictures proportionally
56 ///

57 ///
58 ///
59 ///
60 ///
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 //Scale proportionally
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 //Set the drawing quality of the canvas
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 //Set compression quality
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. Summary:

Above, the simple function of batch zooming (cropping) pictures into 1-inch pictures is completed.

 1      /// 

2 /// Crop button
3 ///

4 ///
5 ///
6 private void Button1_Click(object sender, EventArgs e)
7 {
8 OpenFileDialog dialog = new OpenFileDialog(); < span style="color: #008000;">//Open the folder and select the picture
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); //Settings 1 inch photo
16 string imgSaveUrl = @"D:/My picture/Processing completed/" + dialog.SafeFileName; //Save the picture to the folder
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 }

  1      /// 

2 /// 显示图片
3 ///

4 ///
5 ///
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 ///
42 /// 中间件,写着玩
43 ///

44 ///
45 ///
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 ///
55 /// 等比例缩放图片
56 ///

57 ///
58 ///
59 ///
60 ///
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 }

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3076 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.