加图片水印
来源:互联网 发布:王向远 知乎 编辑:程序博客网 时间:2024/05/29 16:32
调用代码:
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 }
实现代码:
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 else101 {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
<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- jsp加图片水印
- 图片加水印类
- 图片加水印
- 图片加水印类
- ASP图片加水印
- 图片加水印
- 给图片加水印
- 图片加水印
- 图片加水印
- C# 图片加水印
- 给图片加水印
- 图片加水印
- .net图片加水印
- 图片加水印
- 给图片加水印
- 图片加水印
- 给图片加水印
- 图片加水印
- ArcGIS Runtime for Android开发教程V2.0(4)基础篇
- 调用项目淘宝开放平台API调用nodejs实现
- 优化网站设计(二十三):减小Cookie的体积
- 注册验证框(css+div+jquery)
- XMPP协议介绍
- 加图片水印
- 功能搜索js+flash实现手写输入功能特效
- HTML增加自定义属性
- Java四人网络斗地主游戏《客户端》
- Linux min 和 max 宏
- hibernate的各种保存方式的区别 (save,persist,update,saveOrUpdte,merge,flush,lock)等
- 常用webservice网址
- 利用Opencv读取图片RGB值并存储在txt文件中
- DBConnectTest.java