[C#] C#监控文件夹并自动给图片文件打水印 →→→→→进入此内容的聊天室

来自 , 2020-12-10, 写在 C#, 查看 161 次.
URL http://www.code666.cn/view/6d4f95bf
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. namespace FolderWatcher
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.         private static string text = "http://www.cnblogs.com/zhuzhenyu";
  19.         private static string path = @"E:\FolderWatcher";
  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             if (!string.IsNullOrEmpty(this.textBox1.Text))
  23.             {
  24.                 path = this.textBox1.Text;
  25.             }
  26.             if (!string.IsNullOrEmpty(this.textBox2.Text))
  27.             {
  28.                 text = this.textBox2.Text;
  29.             }
  30.             WatcherStrat(path, "*.*");
  31.         }
  32.  
  33.         private static void WatcherStrat(string path, string filter)
  34.         {
  35.  
  36.             FileSystemWatcher watcher = new FileSystemWatcher();
  37.             watcher.Path = path;
  38.             watcher.Filter = filter;
  39.             watcher.Created += new FileSystemEventHandler(OnProcess);
  40.             watcher.EnableRaisingEvents = true;
  41.             watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess
  42.                                    | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
  43.             watcher.IncludeSubdirectories = true;
  44.         }
  45.  
  46.         private static void OnProcess(object source, FileSystemEventArgs e)
  47.         {
  48.             if (e.ChangeType == WatcherChangeTypes.Created)
  49.             {
  50.                 OnCreated(source, e);
  51.             }
  52.         }
  53.         private static void OnCreated(object source, FileSystemEventArgs e)
  54.         {
  55.             if (e.FullPath.IndexOf("_new.") < 0)
  56.             {
  57.                 FinePic(e.FullPath, text, e.FullPath.Replace(".", "_new."), new Font("宋体", 15, FontStyle.Bold));
  58.             }
  59.         }
  60.  
  61.         /// <summary>
  62.         /// 图片水印
  63.         /// </summary>
  64.         /// <param name="FileName">源文件路径</param>
  65.         /// <param name="wText">水印文字</param>
  66.         /// <param name="savePath">保存路径</param>
  67.         /// <param name="font">字体样式</param>
  68.         public static void FinePic(string FileName, string wText, string savePath, Font font)
  69.         {
  70.             Bitmap bmp = new Bitmap(FileName);
  71.             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
  72.  
  73.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  74.             g.DrawString(wText, font, new SolidBrush(Color.FromArgb(70, Color.Red)), 60, bmp.Height - 120);//加水印
  75.             bmp.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  76.         }
  77.     }
  78. }
  79. //csharp/5484

回复 "C#监控文件夹并自动给图片文件打水印"

这儿你可以回复上面这条便签

captcha