当前位置: 移动技术网 > IT编程>开发语言>c# > C#监控文件夹并自动给图片文件打水印的方法

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

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例讲述了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#程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网