c# 把现有图片顶部top50px 处理成白色

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
/// <summary>
       /// 把现有图片顶部50px刷成白色,去水印
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void btnBrushWhite_Click(object sender, EventArgs e)
       {
 
           // Create a new instance of the BackgroundWorker class
           BackgroundWorker worker = new BackgroundWorker();
 
           // Set the DoWork event handler
           worker.DoWork += new DoWorkEventHandler(btnBrushWhite_ClickCore);
 
           // Start the background worker
           worker.RunWorkerAsync();
 
           this.btnBrushWhite.Enabled = false;
       }
 
 
       private async void btnBrushWhite_ClickCore(object sender, DoWorkEventArgs e)
       {
           var appSettings = ConfigurationManager.AppSettings; // Get the appSettings object
 
           var imgDicPath = appSettings["ImgDicPath"];
           var imgDicOutPath = appSettings["ImgDicOutPath"];
           // Get an array of file paths in the directory
           string[] filePaths = Directory.GetFiles(imgDicPath);
           if (!Directory.Exists(imgDicOutPath))
           {
               Directory.CreateDirectory(imgDicOutPath);
           }
 
           this.pbHandleImg.Maximum = filePaths.Length;
 
           var count = 0;
           var startTime = DateTime.Parse("2022-08-02");
 
           for (int i = 0; i < filePaths.Length; i++)
           {
               await Task.Delay(10);
 
               this.pbHandleImg.Value = i + 1;
               var filePath = filePaths[i];
 
               // 获取图片文件大小
               FileInfo fileInfo = new FileInfo(filePath);
               long fileSizeInBytes = fileInfo.Length;
               //获取上次修改时间
               var lastModified = fileInfo.LastWriteTime;
               if (lastModified < startTime || fileSizeInBytes == 0)
               {
                   this.lblTips.Text = filePath + " 无需处理";
                   continue;
               }
               this.lblTips.Text = filePath;
               try
               {
                   using (Image imageCopy = Image.FromFile(filePath))
                   {
                       // Create a graphics object from the image
                       using (Graphics graphics = Graphics.FromImage(imageCopy))
                       {
                           // Create a white brush
                           using (SolidBrush whiteBrush = new SolidBrush(Color.White))
                           {
                               // Fill a rectangle at the top of the image with the white brush
                               graphics.FillRectangle(whiteBrush, 0, 0, imageCopy.Width, 50);
 
                               // Save the modified image to file
                               imageCopy.Save(filePath.Replace(imgDicPath, imgDicOutPath));
                           }
                       }
                        
                   }
                   count++;
               }
               catch (System.Exception ex)
               {
 
                   // Specify the file path and name
                   string path =  AppDomain.CurrentDomain.BaseDirectory+"/log.txt";
 
                   // Open the file in append mode and create it if it doesn't exist
                   using (StreamWriter writer = new StreamWriter(path, true))
                   {
                       // Write the log message to the file
                       writer.WriteLine(DateTime.Now+","+filePath+","+ex.Message);
                   }
                   continue;
               }
 
           }
           this.btnBrushWhite.Enabled = true;
           MessageBox.Show(string.Format("执行完毕,共处理{0}张图片", count));
       }

  

posted @   iDEAAM  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示