C#監(jiān)控文件夾并自動給圖片文件打水印的方法
本文實例講述了C#監(jiān)控文件夾并自動給圖片文件打水印的方法。分享給大家供大家參考。具體分析如下:
個人私心的緣故,經(jīng)常寫一些博客之類的文章,由于看到網(wǎng)絡(luò)上面好多同志轉(zhuǎn)載后不標(biāo)明出處,所以特地寫了這么一個小程序,這個小程序的功能是當(dāng)我在頁面上通過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#程序設(shè)計有所幫助。
相關(guān)文章
C#字符串?dāng)?shù)組轉(zhuǎn)換為整形數(shù)組的方法
這篇文章主要介紹了C#字符串?dāng)?shù)組轉(zhuǎn)換為整形數(shù)組的方法,涉及C#數(shù)組遍歷與轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-06-06
C# Stream 和 byte[] 之間的轉(zhuǎn)換
Stream 和 byte[] 之間的轉(zhuǎn)換2008-03-03
C#用websocket實現(xiàn)簡易聊天功能(服務(wù)端)
這篇文章主要為大家詳細介紹了C#用websocket實現(xiàn)簡易聊天功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02

