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

 /// <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 @ 2023-04-12 16:15  iDEAAM  阅读(35)  评论(0编辑  收藏  举报