加图片水印

调用代码:

 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

 

posted @ 2013-05-18 17:39  冰vs焰  阅读(202)  评论(0编辑  收藏  举报