C#监控文件夹并自动给图片文件打水印的方法

本文实例讲述了C#监控文件夹并自动给图片文件打水印的方法。分享给大家供大家参考。具体分析如下:

个人私心的缘故,经常写一些博客之类的文章,由于看到网络上面好多同志转载后不标明出处,所以特地写了这么一个小程序,这个小程序的功能是当我在页面上通过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#程序设计有所帮助。

(0)

相关推荐

  • 深入分析WPF客户端读取高清图片卡以及缩略图的解决方法详解

    在Ftp上传上,有人上传了高清图片,每张图片大约2M.如果使用传统的BitmapImage类,然后绑定 Source 属性的方法,有些电脑在首次会比较卡,一张电脑10秒,4张大约会卡40秒. 所以我先异步的下载图片,得到downloadFileStream对象,然后绑定到BitmapImage类上.例如:System.Windows.Controls.Image photo = new Image{    Width = 100,    Height = 100,    Margin = new

  • C# 添加图片水印类实现代码

    复制代码 代码如下: using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.IO; using System.Drawing.Imaging; using System.Web; using System.Drawing.Drawing2D; using System.Reflection; namespace Chen { public clas

  • WPF 自定义雷达图开发实例教程

    自定义雷达图表如下: 1.创建UserControl,名为"RadarChartControl" 前台: <UserControl x:Class="WpfApplication2.RadarChartControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/win

  • 在WinForm和WPF中使用GMap.Net地图插件简单教程

    如何在WinForm中使用GMap.Net 项目主页:https://greatmaps.codeplex.com/ 下载GMap.Net,我下载的版本:greatmaps_81b71bf30091,编译三个核心项目: GMap.Net.Core:核心DLL GMap.Net.WindowsForms:WinForm中使用的DLL GMap.NET.WindowsPresentation:WPF中使用的DLL 在WinForm项目中使用GMap: 1.新建一个Visual C# 的Windows

  • c#图片添加水印的实例代码

    复制代码 代码如下: using System;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;using System.IO;namespace ImageDrawing{ /// <summary> /// 图片修改类,主要是用来保护图片版权的 /// </summary> public class ImageModification {  #region &quo

  • C#(.net)水印图片的生成完整实例

    本文以一个完整实例讲述了C#水印图片的生成方法.是非常实用的技巧.分享给大家供大家参考. 具体实例代码如下: /* * * 使用说明: * 建议先定义一个WaterImage实例 * 然后利用实例的属性,去匹配需要进行操作的参数 * 然后定义一个WaterImageManage实例 * 利用WaterImageManage实例进行DrawImage(),印图片水印 * DrawWords()印文字水印 * */ using System; using System.Drawing; using

  • .net c# gif动画如何添加图片水印实现思路及代码

    复制代码 代码如下: public static Bitmap WaterMarkWithText(System.Drawing.Bitmap origialGif, string text,string filePath) { //用于存放桢 List<Frame> frames = new List<Frame>(); //如果不是gif文件,直接返回原图像 if (origialGif.RawFormat.Guid != System.Drawing.Imaging.Imag

  • C#给图片加水印的简单实现方法

    本文实例讲述了C#给图片加水印的简单实现方法.分享给大家供大家参考.具体分析如下: 这里实现本网站图片保护功能类: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Drawing;//image的命名空间 namespace 实现本网站图片保护功能 { public class yanzhengma:IHttpHandler { public boo

  • WPF实现图片合成或加水印的方法【2种方法】

    本文实例讲述了WPF实现图片合成或加水印的方法.分享给大家供大家参考,具体如下: 最近项目中应用多次应用了图片合成,为了今后方便特此记下. 在WPF下有两种图片合成的方式,一种还是用原来C#提供的GDI+方式,命名空间是System.Drawing 和 System.Drawing.Imaging,另一种是WPF中新添加的API,命名空间是 System.Windows.Media 和 System.Windows.Media.Imaging . 我们来做一个简单的例子,分别用上面的两种方式实现

  • C#给图片添加水印完整实例

    本文实例讲述了C#给图片添加水印的方法.分享给大家供大家参考,具体如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Syste

  • WPF TextBox和PasswordBox添加水印

    本文实例为大家分享TextBox和PasswordBox加水印的方法,供大家参考,具体内容如下 Textbox加水印 Textbox加水印,需要一个VisualBrush和触发器验证Text是否为空,在空的时候设置背景的Brush就可以实现水印效果. <TextBox Name="txtBoxName" Width="120" Height="23"> <TextBox.Resources> <VisualBrush

随机推荐