• 加图片水印


    调用代码:

     1  try
     2             {
     3                 if (!string.IsNullOrEmpty(DirFile))
     4                 {
     5                     List<string> dirs = ImageFile(DirFile);
     6                     foreach (var item in dirs)
     7                     {
     8                         ImageWatermark(item, water, comboBox1.SelectedItem.ToString());
     9                       
    10                     }
    11                     MessageBox.Show("批量设置完成!");
    12                 }
    13 
    14                 if (!string.IsNullOrEmpty(ItemFile))
    15                 {
    16                     ImageWatermark(ItemFile, water, comboBox1.SelectedItem.ToString());
    17                     MessageBox.Show("单个设置完成!");
    18                 }
    19             }
    20             catch (Exception ex)
    21             {
    22                 MessageBox.Show(ex.Message);
    23             }
    View Code

    实现代码:

      1 #region 图片水印
      2         /// <summary>
      3         /// 图片水印处理方法
      4         /// </summary>
      5         /// <param name="path">需要加载水印的图片路径(绝对路径)</param>
      6         /// <param name="waterpath">水印图片(绝对路径)</param>
      7         /// <param name="location">水印位置(传送正确的代码)</param>
      8         public static string ImageWatermark(string path, string waterpath, string location)
      9         {
     10             string kz_name = Path.GetExtension(path);
     11             if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg")
     12             {
     13                 DateTime time = DateTime.Now;
     14                 string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString();
     15                 Image img = Bitmap.FromFile(path);
     16                 Image waterimg = Image.FromFile(waterpath);
     17                 Graphics g = Graphics.FromImage(img);
     18                 ArrayList loca = GetLocation(location, img, waterimg);
     19                 g.DrawImage(waterimg, new Rectangle(int.Parse(loca[0].ToString()), int.Parse(loca[1].ToString()), waterimg.Width, waterimg.Height));
     20                 waterimg.Dispose();
     21                 g.Dispose();
     22                 string newpath;
     23                 bool isEquall = false;
     24                 if (savePath == Path.GetDirectoryName(path))
     25                 {
     26                     newpath = Path.GetDirectoryName(path) + filename + kz_name;
     27                     isEquall = true;
     28                 }
     29                 else
     30                 {
     31                     newpath = savePath + "\\" + Path.GetFileName(path);
     32                 }
     33                 img.Save(newpath);
     34                 img.Dispose();
     35                 if (isEquall)
     36                 {
     37                     File.Copy(newpath, path, true);
     38                     if (File.Exists(newpath))
     39                     {
     40                         File.Delete(newpath);
     41                     }
     42                 }
     43             }
     44             return path;
     45         }
     46 
     47         /// <summary>
     48         /// 图片水印位置处理方法
     49         /// </summary>
     50         /// <param name="location">水印位置</param>
     51         /// <param name="img">需要添加水印的图片</param>
     52         /// <param name="waterimg">水印图片</param>
     53         private static ArrayList GetLocation(string location, Image img, Image waterimg)
     54         {
     55             ArrayList loca = new ArrayList();
     56             int x = 0;
     57             int y = 0;
     58 
     59             if (location == "左上")
     60             {
     61                 x = 10;
     62                 y = 10;
     63             }
     64             else if (location == "顶部")
     65             {
     66                 x = img.Width / 2 - waterimg.Width / 2;
     67                // y = img.Height - waterimg.Height;
     68                 y = 10;
     69             }
     70             else if (location == "右上")
     71             {
     72                 x = img.Width - waterimg.Width;
     73                 y = 10;
     74             }
     75             else if (location == "左中")
     76             {
     77                 x = 10;
     78                 y = img.Height / 2 - waterimg.Height / 2;
     79             }
     80             else if (location == "中间")
     81             {
     82                 x = img.Width / 2 - waterimg.Width / 2;
     83                 y = img.Height / 2 - waterimg.Height / 2;
     84             }
     85             else if (location == "右中")
     86             {
     87                 x = img.Width - waterimg.Width;
     88                 y = img.Height / 2 - waterimg.Height / 2;
     89             }
     90             else if (location == "左下")
     91             {
     92                 x = 10;
     93                 y = img.Height - waterimg.Height;
     94             }
     95             else if (location == "底部")
     96             {
     97                 x = img.Width / 2 - waterimg.Width / 2;
     98                 y = img.Height - waterimg.Height;
     99             }
    100             else
    101             {
    102                 x = img.Width - waterimg.Width;
    103                 y = img.Height - waterimg.Height;
    104             }
    105             loca.Add(x);
    106             loca.Add(y);
    107             return loca;
    108         }
    109         #endregion
    View Code
  • 相关阅读:
    [转]list的交集,差集,并集
    [转]$.post() 和 $.get() 如何同步请求
    [转]Jsoup(一)Jsoup详解(官方)
    [转]Kindeditor图片粘贴上传(chrome)
    [转]kindeditor隐藏上传图片框网络图片或本地上传的功能
    微信公众号平台上传文件返回错误代码:40005 invalid file type
    [转]spring MultipartFile 转 File
    [转]客户端js判断文件类型和文件大小即限制上传大小
    java list排序
    spring security oauth2.0 实现
  • 原文地址:https://www.cnblogs.com/lihui1030/p/3085713.html
Copyright © 2020-2023  润新知