C#監(jiān)控文件夾并自動給圖片文件打水印的方法
更新時間:2015年05月19日 14:41:02 作者:振宇爸爸
這篇文章主要介紹了C#監(jiān)控文件夾并自動給圖片文件打水印的方法,涉及C#針對文件夾及圖片操作的相關技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了C#監(jiān)控文件夾并自動給圖片文件打水印的方法。分享給大家供大家參考。具體分析如下:
個人私心的緣故,經(jīng)常寫一些博客之類的文章,由于看到網(wǎng)絡上面好多同志轉(zhuǎn)載后不標明出處,所以特地寫了這么一個小程序,這個小程序的功能是當我在頁面上通過QQ截圖之后,把截到的圖片保存到一個指定的路徑,然后工具自動幫我把圖片上面加上水印。
下面是全部代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace FolderWatcher
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private static string text = "http://www.cnblogs.com/zhuzhenyu";
private static string path = @"E:\FolderWatcher";
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.textBox1.Text))
{
path = this.textBox1.Text;
}
if (!string.IsNullOrEmpty(this.textBox2.Text))
{
text = this.textBox2.Text;
}
WatcherStrat(path, "*.*");
}
private static void WatcherStrat(string path, string filter)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = path;
watcher.Filter = filter;
watcher.Created += new FileSystemEventHandler(OnProcess);
watcher.EnableRaisingEvents = true;
watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess
| NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
watcher.IncludeSubdirectories = true;
}
private static void OnProcess(object source, FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Created)
{
OnCreated(source, e);
}
}
private static void OnCreated(object source, FileSystemEventArgs e)
{
if (e.FullPath.IndexOf("_new.") < 0)
{
FinePic(e.FullPath, text, e.FullPath.Replace(".", "_new."), new Font("宋體", 15, FontStyle.Bold));
}
}
/// <summary>
/// 圖片水印
/// </summary>
/// <param name="FileName">源文件路徑</param>
/// <param name="wText">水印文字</param>
/// <param name="savePath">保存路徑</param>
/// <param name="font">字體樣式</param>
public static void FinePic(string FileName, string wText, string savePath, Font font)
{
Bitmap bmp = new Bitmap(FileName);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.DrawString(wText, font, new SolidBrush(Color.FromArgb(70, Color.Red)), 60, bmp.Height - 120);//加水印
bmp.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
來看一下效果


這里的代碼非常簡單,大家不要噴我
我是一只辛勤耕耘的螞蟻
希望本文所述對大家的C#程序設計有所幫助。
相關文章
C#刪除只讀文件或文件夾(解決File.Delete無法刪除文件)
這篇文章主要介紹了C#刪除只讀文件或文件夾(解決File.Delete無法刪除文件),需要的朋友可以參考下2015-09-09

