ASP.NET(C#)图片加文字、图片水印

一、图片上加文字:

  1. //using System.Drawing;
  2. //using System.IO;
  3. //using System.Drawing.Imaging;
  4.  
  5. private void AddTextToImg(string fileName,string text)
  6. {
  7. if(!File.Exists(MapPath(fileName)))
  8. {
  9. throw new FileNotFoundException("The file don't exist!");
  10. }
  11.  
  12. if( text == string.Empty )
  13. {
  14. return;
  15. }
  16. //还需要判断文件类型是否为图像类型,这里就不赘述了
  17.  
  18. System.Drawing.Image image = System.Drawing.Image.FromFile(MapPath(fileName));
  19. Bitmap bitmap = new Bitmap(image,image.Width,image.Height);
  20. Graphics g = Graphics.FromImage(bitmap);
  21.  
  22. float fontSize = 12.0f; //字体大小
  23. float textWidth = text.Length*fontSize; //文本的长度
  24. //下面定义一个矩形区域,以后在这个矩形里画上白底黑字
  25. float rectX = ;
  26. float rectY = ;
  27. float rectWidth = text.Length*(fontSize+);
  28. float rectHeight = fontSize+;
  29. //声明矩形域
  30. RectangleF textArea = new RectangleF(rectX,rectY,rectWidth,rectHeight);
  31.  
  32. Font font = new Font("宋体",fontSize); //定义字体
  33. Brush whiteBrush = new SolidBrush(Color.White); //白笔刷,画文字用
  34. Brush blackBrush = new SolidBrush(Color.Black); //黑笔刷,画背景用
  35.  
  36. g.FillRectangle(blackBrush,rectX,rectY,rectWidth,rectHeight);
  37.  
  38. g.DrawString(text,font,whiteBrush,textArea);
  39. MemoryStream ms = new MemoryStream( );
  40. //保存为Jpg类型
  41. bitmap.Save(ms,ImageFormat.Jpeg);
  42.  
  43. //输出处理后的图像,这里为了演示方便,我将图片显示在页面中了
  44. Response.Clear();
  45. Response.ContentType = "image/jpeg";
  46. Response.BinaryWrite( ms.ToArray() );
  47.  
  48. g.Dispose();
  49. bitmap.Dispose();
  50. image.Dispose();
  51. }

二、图片上加水印:

  1. //using System;
  2. //using System.Web;
  3. //using System.Drawing;
  4. //using System.Drawing.Drawing2D;
  5. //using System.Drawing.Imaging;
  6. //using System.IO;
  7. //using System.Reflection;
  8.  
  9. /// <summary>
  10. /// 给图片上水印
  11. /// </summary>
  12. /// <param name="filePath">原图片地址</param>
  13. /// <param name="waterFile">水印图片地址</param>
  14. public void MarkWater(string filePath,string waterFile)
  15. {
  16. //GIF不水印
  17. int i = filePath.LastIndexOf(".");
  18. string ex = filePath.Substring(i,filePath.Length - i);
  19. if(string.Compare(ex,".gif",true) == )
  20. {
  21. return;
  22. }
  23.  
  24. string ModifyImagePath = BasePath + filePath;//修改的图像路径
  25. int lucencyPercent=;
  26. Image modifyImage=null;
  27. Image drawedImage=null;
  28. Graphics g=null;
  29. try
  30. {
  31. //建立图形对象
  32. modifyImage=Image.FromFile(ModifyImagePath,true);
  33. drawedImage=Image.FromFile(BasePath + waterFile,true);
  34. g=Graphics.FromImage(modifyImage);
  35. //获取要绘制图形坐标
  36. int x=modifyImage.Width-drawedImage.Width;
  37. int y=modifyImage.Height-drawedImage.Height;
  38. //设置颜色矩阵
  39. float[][] matrixItems ={
  40. new float[] {, , , , },
  41. new float[] {, , , , },
  42. new float[] {, , , , },
  43. new float[] {, , , (float)lucencyPercent/100f, },
  44. new float[] {, , , , }};
  45.  
  46. ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
  47. ImageAttributes imgAttr=new ImageAttributes();
  48. imgAttr.SetColorMatrix(colorMatrix,ColorMatrixFlag.Default,ColorAdjustType.Bitmap);
  49. //绘制阴影图像
  50. g.DrawImage(drawedImage,new Rectangle(x,y,drawedImage.Width,drawedImage.Height),,,drawedImage.Width,drawedImage.Height,GraphicsUnit.Pixel,imgAttr);
  51. //保存文件
  52. string[] allowImageType={".jpg",".gif",".png",".bmp",".tiff",".wmf",".ico"};
  53. FileInfo fi=new FileInfo(ModifyImagePath);
  54. ImageFormat imageType=ImageFormat.Gif;
  55. switch(fi.Extension.ToLower())
  56. {
  57. case ".jpg": imageType=ImageFormat.Jpeg; break;
  58. case ".gif": imageType=ImageFormat.Gif; break;
  59. case ".png": imageType=ImageFormat.Png; break;
  60. case ".bmp": imageType=ImageFormat.Bmp; break;
  61. case ".tif": imageType=ImageFormat.Tiff; break;
  62. case ".wmf": imageType=ImageFormat.Wmf; break;
  63. case ".ico": imageType=ImageFormat.Icon; break;
  64. default: break;
  65. }
  66. MemoryStream ms=new MemoryStream();
  67. modifyImage.Save(ms,imageType);
  68. byte[] imgData=ms.ToArray();
  69. modifyImage.Dispose();
  70. drawedImage.Dispose();
  71. g.Dispose();
  72. FileStream fs=null;
  73. File.Delete(ModifyImagePath);
  74. fs=new FileStream(ModifyImagePath,FileMode.Create,FileAccess.Write);
  75. if(fs!=null)
  76. {
  77. fs.Write(imgData,,imgData.Length);
  78. fs.Close();
  79. }
  80. }
  81. finally
  82. {
  83. try
  84. {
  85. drawedImage.Dispose();
  86. modifyImage.Dispose();
  87. g.Dispose();
  88. }
  89. catch
  90. {
  91. }
  92. }
  93. }

三、 图片+水印(透明)函数

  1. <summary>
  2. /// 添加图片水印
  3. /// </summary>
  4. /// <param name="path">原图片绝对地址</param>
  5. /// <param name="Ext">文件后缀</param>
  6. public static string addWaterMark(string path,string fileExt)
  7. {
  8. System.Drawing.Image image = System.Drawing.Image.FromFile(path);
  9. Bitmap b = new Bitmap(image.Width, image.Height,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  10. Graphics g = Graphics.FromImage(b);
  11. g.Clear(Color.White);
  12. g.DrawImage(image, , , image.Width, image.Height);
  13.  
  14. Image watermark = new Bitmap(ConfigurationSettings.AppSettings["PersonalPath"] + @"images/logo.jpg");
  15.  
  16. System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes();
  17. System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap();
  18. colorMap.OldColor = Color.FromArgb(, , , );
  19. colorMap.NewColor = Color.FromArgb(, , , );
  20. System.Drawing.Imaging.ColorMap[] remapTable = {colorMap};
  21. imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap);
  22. float[][] colorMatrixElements = {
  23. new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
  24. new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
  25. new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
  26. new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f},
  27. new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
  28. };
  29. System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);
  30. imageAttributes.SetColorMatrix(colorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);
  31. int xpos = ;
  32. int ypos = ;
  33.  
  34. xpos = ((image.Width - watermark.Width) - );
  35. ypos = image.Height - watermark.Height - ;
  36.  
  37. g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), , , watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
  38.  
  39. watermark.Dispose();
  40. imageAttributes.Dispose();
  41.  
  42. //保存加水印过后的图片,删除原始图片
  43. Random ro=new Random((int)DateTime.Now.Ticks);
  44. string temppath = System.DateTime.Now.ToString("yyyy") + "//" + System.DateTime.Now.ToString("MMdd");
  45. string fileName = temppath + "//" + DateTime.Now.ToString("yyyyMMddHHmmss") + ro.Next() + fileExt;
  46. string filepath = Functions.GetUserFactPath(User.GetUserFromSession().Adddate,User.GetUserFromSession().UserId) + "3//";
  47.  
  48. b.Save(filepath + fileName);
  49. b.Dispose();
  50. image.Dispose();
  51. if(File.Exists(path))
  52. {
  53. File.Delete(path);
  54. }
  55.  
  56. return fileName;
  57. }

四、C#图片水印生成类(图片、文字、透明水印)

  1. /*
  2. * Class:WaterImage
  3. * Use for add a water Image to the picture both words and image
  4. * 2007.07.23 create the file
  5. *
  6. * http://www.freeatom.com/ 欢迎您的来访
  7. *
  8. * 使用说明:
  9. *  建议先定义一个WaterImage实例
  10. *  然后利用实例的属性,去匹配需要进行操作的参数
  11. *  然后定义一个WaterImageManage实例
  12. *  利用WaterImageManage实例进行DrawImage(),印图片水印
  13. *  DrawWords()印文字水印
  14. *
  15. -*/
  16.  
  17. using System;
  18. using System.Drawing;
  19. using System.Drawing.Imaging;
  20. using System.Drawing.Drawing2D;
  21. using System.IO;
  22. /// <summary>
  23. /// 图片位置
  24. /// </summary>
  25. public enum ImagePosition
  26. {
  27. LeftTop, //左上
  28. LeftBottom, //左下
  29. RightTop, //右上
  30. RigthBottom, //右下
  31. TopMiddle, //顶部居中
  32. BottomMiddle, //底部居中
  33. Center //中心
  34. }
  35.  
  36. /// <summary>
  37. /// 水印图片的操作管理 Design by Gary Gong From Demetersoft.com
  38. /// </summary>
  39. public class WaterImageManage
  40. {
  41. /// <summary>
  42. /// 生成一个新的水印图片制作实例
  43. /// </summary>
  44. public WaterImageManage ()
  45. {
  46. //
  47. // TODO: Add constructor logic here
  48. //
  49. }
  50.  
  51. /// <summary>
  52. /// 添加图片水印
  53. /// </summary>
  54. /// <param name="sourcePicture">源图片文件名</param>
  55. /// <param name="waterImage">水印图片文件名</param>
  56. /// <param name="alpha">透明度(0.1-1.0数值越小透明度越高)</param>
  57. /// <param name="position">位置</param>
  58. /// <param name="PicturePath" >图片的路径</param>
  59. /// <returns>返回生成于指定文件夹下的水印文件名</returns>
  60. public string DrawImage(string sourcePicture,
  61. string waterImage,
  62. float alpha,
  63. ImagePosition position,
  64. string PicturePath )
  65. {
  66. //
  67. // 判断参数是否有效
  68. //
  69. if (sourcePicture == string.Empty || waterImage == string.Empty || alpha == 0.0 || PicturePath == string.Empty)
  70. {
  71. return sourcePicture;
  72. }
  73.  
  74. //
  75. // 源图片,水印图片全路径
  76. //
  77. string sourcePictureName = PicturePath + sourcePicture;
  78. string waterPictureName = PicturePath + waterImage;
  79. string fileSourceExtension = System.IO.Path.GetExtension(sourcePictureName).ToLower();
  80. string fileWaterExtension = System.IO.Path.GetExtension(waterPictureName).ToLower();
  81. //
  82. // 判断文件是否存在,以及类型是否正确
  83. //
  84. if (System.IO.File.Exists(sourcePictureName) == false ||
  85. System.IO.File.Exists(waterPictureName) == false ||(
  86. fileSourceExtension != ".gif" &&
  87. fileSourceExtension != ".jpg" &&
  88. fileSourceExtension != ".png") || (
  89. fileWaterExtension != ".gif" &&
  90. fileWaterExtension != ".jpg" &&
  91. fileWaterExtension != ".png")
  92. )
  93. {
  94. return sourcePicture;
  95. }
  96.  
  97. //
  98. // 目标图片名称及全路径
  99. //
  100. string targetImage = sourcePictureName.Replace ( System.IO.Path.GetExtension(sourcePictureName),"") + "_1101.jpg";
  101.  
  102. //
  103. // 将需要加上水印的图片装载到Image对象中
  104. //
  105. Image imgPhoto = Image.FromFile(sourcePictureName);
  106. //
  107. // 确定其长宽
  108. //
  109. int phWidth = imgPhoto.Width;
  110. int phHeight = imgPhoto.Height;
  111.  
  112. //
  113. // 封装 GDI+ 位图,此位图由图形图像及其属性的像素数据组成。
  114. //
  115. Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
  116.  
  117. //
  118. // 设定分辨率
  119. //
  120. bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
  121.  
  122. //
  123. // 定义一个绘图画面用来装载位图
  124. //
  125. Graphics grPhoto = Graphics.FromImage(bmPhoto);
  126.  
  127. //
  128. //同样,由于水印是图片,我们也需要定义一个Image来装载它
  129. //
  130. Image imgWatermark = new Bitmap(waterPictureName);
  131.  
  132. //
  133. // 获取水印图片的高度和宽度
  134. //
  135. int wmWidth = imgWatermark.Width;
  136. int wmHeight = imgWatermark.Height;
  137.  
  138. //SmoothingMode:指定是否将平滑处理(消除锯齿)应用于直线、曲线和已填充区域的边缘。
  139. // 成员名称 说明
  140. // AntiAlias 指定消除锯齿的呈现。
  141. // Default 指定不消除锯齿。
  142. // HighQuality 指定高质量、低速度呈现。
  143. // HighSpeed 指定高速度、低质量呈现。
  144. // Invalid 指定一个无效模式。
  145. // None 指定不消除锯齿。
  146. grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
  147.  
  148. //
  149. // 第一次描绘,将我们的底图描绘在绘图画面上
  150. //
  151. grPhoto.DrawImage(imgPhoto,
  152. new Rectangle(, , phWidth, phHeight),
  153. ,
  154. ,
  155. phWidth,
  156. phHeight,
  157. GraphicsUnit.Pixel);
  158.  
  159. //
  160. // 与底图一样,我们需要一个位图来装载水印图片。并设定其分辨率
  161. //
  162. Bitmap bmWatermark = new Bitmap(bmPhoto);
  163. bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
  164.  
  165. //
  166. // 继续,将水印图片装载到一个绘图画面grWatermark
  167. //
  168. Graphics grWatermark = Graphics.FromImage(bmWatermark);
  169.  
  170. //
  171. //ImageAttributes 对象包含有关在呈现时如何操作位图和图元文件颜色的信息。
  172. //
  173. ImageAttributes imageAttributes = new ImageAttributes();
  174.  
  175. //
  176. //Colormap: 定义转换颜色的映射
  177. //
  178. ColorMap colorMap = new ColorMap();
  179.  
  180. //
  181. //我的水印图被定义成拥有绿色背景色的图片被替换成透明
  182. //
  183. colorMap.OldColor = Color.FromArgb(, , , );
  184. colorMap.NewColor = Color.FromArgb(, , , );
  185.  
  186. ColorMap[] remapTable = { colorMap };
  187.  
  188. imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
  189.  
  190. float[][] colorMatrixElements = {
  191. new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, // red红色
  192. new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, //green绿色
  193. new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, //blue蓝色
  194. new float[] {0.0f, 0.0f, 0.0f, alpha, 0.0f}, //透明度
  195. new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};//
  196.  
  197. // ColorMatrix:定义包含 RGBA 空间坐标的 5 x 5 矩阵。
  198. // ImageAttributes 类的若干方法通过使用颜色矩阵调整图像颜色。
  199. ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
  200.  
  201. imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
  202. ColorAdjustType.Bitmap);
  203.  
  204. //
  205. //上面设置完颜色,下面开始设置位置
  206. //
  207. int xPosOfWm;
  208. int yPosOfWm;
  209.  
  210. switch (position)
  211. {
  212. case ImagePosition .BottomMiddle :
  213. xPosOfWm = (phWidth-wmWidth ) / ;
  214. yPosOfWm = phHeight- wmHeight -;
  215. break ;
  216. case ImagePosition .Center :
  217. xPosOfWm = (phWidth - wmWidth) / ;
  218. yPosOfWm = (phHeight-wmHeight ) / ;
  219. break ;
  220. case ImagePosition .LeftBottom :
  221. xPosOfWm = ;
  222. yPosOfWm = phHeight - wmHeight - ;
  223. break ;
  224. case ImagePosition .LeftTop :
  225. xPosOfWm = ;
  226. yPosOfWm = ;
  227. break;
  228. case ImagePosition .RightTop :
  229. xPosOfWm = phWidth - wmWidth - ;
  230. yPosOfWm = ;
  231. break ;
  232. case ImagePosition .RigthBottom :
  233. xPosOfWm = phWidth - wmWidth - ;
  234. yPosOfWm = phHeight - wmHeight - ;
  235. break ;
  236. case ImagePosition.TopMiddle :
  237. xPosOfWm = (phWidth - wmWidth) / ;
  238. yPosOfWm = ;
  239. break ;
  240. default:
  241. xPosOfWm = ;
  242. yPosOfWm = phHeight - wmHeight - ;
  243. break;
  244. }
  245.  
  246. //
  247. // 第二次绘图,把水印印上去
  248. //
  249. grWatermark.DrawImage(imgWatermark,
  250. new Rectangle(xPosOfWm,
  251. yPosOfWm,
  252. wmWidth,
  253. wmHeight),
  254. ,
  255. ,
  256. wmWidth,
  257. wmHeight,
  258. GraphicsUnit.Pixel,
  259. imageAttributes);
  260.  
  261. imgPhoto = bmWatermark;
  262. grPhoto.Dispose();
  263. grWatermark.Dispose();
  264.  
  265. //
  266. // 保存文件到服务器的文件夹里面
  267. //
  268. imgPhoto.Save(targetImage, ImageFormat.Jpeg);
  269. imgPhoto.Dispose();
  270. imgWatermark.Dispose();
  271. return targetImage.Replace (PicturePath,"");
  272. }
  273.  
  274. /// <summary>
  275. /// 在图片上添加水印文字
  276. /// </summary>
  277. /// <param name="sourcePicture">源图片文件</param>
  278. /// <param name="waterWords">需要添加到图片上的文字</param>
  279. /// <param name="alpha">透明度</param>
  280. /// <param name="position">位置</param>
  281. /// <param name="PicturePath">文件路径</param>
  282. /// <returns></returns>
  283. public string DrawWords(string sourcePicture,
  284. string waterWords,
  285. float alpha,
  286. ImagePosition position,
  287. string PicturePath)
  288. {
  289. //
  290. // 判断参数是否有效
  291. //
  292. if (sourcePicture == string.Empty || waterWords == string.Empty || alpha == 0.0 || PicturePath == string.Empty)
  293. {
  294. return sourcePicture;
  295. }
  296.  
  297. //
  298. // 源图片全路径
  299. //
  300. string sourcePictureName = PicturePath + sourcePicture;
  301. string fileExtension = System.IO.Path.GetExtension(sourcePictureName).ToLower();
  302.  
  303. //
  304. // 判断文件是否存在,以及文件名是否正确
  305. //
  306. if (System.IO.File.Exists(sourcePictureName) == false || (
  307. fileExtension != ".gif" &&
  308. fileExtension != ".jpg" &&
  309. fileExtension != ".png" ))
  310. {
  311. return sourcePicture;
  312. }
  313.  
  314. //
  315. // 目标图片名称及全路径
  316. //
  317. string targetImage = sourcePictureName.Replace(System.IO.Path.GetExtension(sourcePictureName), "") + "_0703.jpg";
  318.  
  319. //创建一个图片对象用来装载要被添加水印的图片
  320. Image imgPhoto = Image.FromFile(sourcePictureName);
  321.  
  322. //获取图片的宽和高
  323. int phWidth = imgPhoto.Width;
  324. int phHeight = imgPhoto.Height;
  325.  
  326. //
  327. //建立一个bitmap,和我们需要加水印的图片一样大小
  328. Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
  329.  
  330. //SetResolution:设置此 Bitmap 的分辨率
  331. //这里直接将我们需要添加水印的图片的分辨率赋给了bitmap
  332. bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
  333.  
  334. //Graphics:封装一个 GDI+ 绘图图面。
  335. Graphics grPhoto = Graphics.FromImage(bmPhoto);
  336.  
  337. //设置图形的品质
  338. grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
  339.  
  340. //将我们要添加水印的图片按照原始大小描绘(复制)到图形中
  341. grPhoto.DrawImage(
  342. imgPhoto, // 要添加水印的图片
  343. new Rectangle(, , phWidth, phHeight), // 根据要添加的水印图片的宽和高
  344. , // X方向从0点开始描绘
  345. , // Y方向
  346. phWidth, // X方向描绘长度
  347. phHeight, // Y方向描绘长度
  348. GraphicsUnit.Pixel); // 描绘的单位,这里用的是像素
  349.  
  350. //根据图片的大小我们来确定添加上去的文字的大小
  351. //在这里我们定义一个数组来确定
  352. int[] sizes = new int[] { , , , , , , };
  353.  
  354. //字体
  355. Font crFont = null;
  356. //矩形的宽度和高度,SizeF有三个属性,分别为Height高,width宽,IsEmpty是否为空
  357. SizeF crSize = new SizeF();
  358.  
  359. //利用一个循环语句来选择我们要添加文字的型号
  360. //直到它的长度比图片的宽度小
  361. for (int i = ; i < ; i++)
  362. {
  363. crFont = new Font("arial", sizes[i], FontStyle.Bold);
  364.  
  365. //测量用指定的 Font 对象绘制并用指定的 StringFormat 对象格式化的指定字符串。
  366. crSize = grPhoto.MeasureString(waterWords, crFont);
  367.  
  368. // ushort 关键字表示一种整数数据类型
  369. if ((ushort)crSize.Width < (ushort)phWidth)
  370. break;
  371. }
  372.  
  373. //截边5%的距离,定义文字显示(由于不同的图片显示的高和宽不同,所以按百分比截取)
  374. int yPixlesFromBottom = (int)(phHeight * .);
  375.  
  376. //定义在图片上文字的位置
  377. float wmHeight = crSize.Height;
  378. float wmWidth = crSize .Width ;
  379.  
  380. float xPosOfWm;
  381. float yPosOfWm;
  382.  
  383. switch (position)
  384. {
  385. case ImagePosition .BottomMiddle :
  386. xPosOfWm = phWidth / ;
  387. yPosOfWm = phHeight- wmHeight -;
  388. break ;
  389. case ImagePosition .Center :
  390. xPosOfWm = phWidth / ;
  391. yPosOfWm = phHeight / ;
  392. break ;
  393. case ImagePosition .LeftBottom :
  394. xPosOfWm = wmWidth;
  395. yPosOfWm = phHeight - wmHeight - ;
  396. break ;
  397. case ImagePosition .LeftTop :
  398. xPosOfWm = wmWidth/ ;
  399. yPosOfWm = wmHeight / ;
  400. break;
  401. case ImagePosition .RightTop :
  402. xPosOfWm = phWidth - wmWidth - ;
  403. yPosOfWm = wmHeight;
  404. break ;
  405. case ImagePosition .RigthBottom :
  406. xPosOfWm = phWidth - wmWidth - ;
  407. yPosOfWm = phHeight - wmHeight - ;
  408. break ;
  409. case ImagePosition.TopMiddle :
  410. xPosOfWm = phWidth / ;
  411. yPosOfWm = wmWidth;
  412. break ;
  413. default:
  414. xPosOfWm = wmWidth;
  415. yPosOfWm = phHeight - wmHeight - ;
  416. break;
  417. }
  418.  
  419. //封装文本布局信息(如对齐、文字方向和 Tab 停靠位),显示操作(如省略号插入和国家标准 (National) 数字替换)和 OpenType 功能。
  420. StringFormat StrFormat = new StringFormat();
  421.  
  422. //定义需要印的文字居中对齐
  423. StrFormat.Alignment = StringAlignment.Center;
  424.  
  425. //SolidBrush:定义单色画笔。画笔用于填充图形形状,如矩形、椭圆、扇形、多边形和封闭路径。
  426. //这个画笔为描绘阴影的画笔,呈灰色
  427. int m_alpha = Convert .ToInt32 ( * alpha);
  428. SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(m_alpha, , , ));
  429.  
  430. //描绘文字信息,这个图层向右和向下偏移一个像素,表示阴影效果
  431. //DrawString 在指定矩形并且用指定的 Brush 和 Font 对象绘制指定的文本字符串。
  432. grPhoto.DrawString(waterWords, //string of text
  433. crFont, //font
  434. semiTransBrush2, //Brush
  435. new PointF(xPosOfWm + , yPosOfWm + ), //Position
  436. StrFormat);
  437.  
  438. //从四个 ARGB 分量(alpha、红色、绿色和蓝色)值创建 Color 结构,这里设置透明度为153
  439. //这个画笔为描绘正式文字的笔刷,呈白色
  440. SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(, , , ));
  441.  
  442. //第二次绘制这个图形,建立在第一次描绘的基础上
  443. grPhoto.DrawString(waterWords, //string of text
  444. crFont, //font
  445. semiTransBrush, //Brush
  446. new PointF(xPosOfWm, yPosOfWm), //Position
  447. StrFormat);
  448.  
  449. //imgPhoto是我们建立的用来装载最终图形的Image对象
  450. //bmPhoto是我们用来制作图形的容器,为Bitmap对象
  451. imgPhoto = bmPhoto;
  452. //释放资源,将定义的Graphics实例grPhoto释放,grPhoto功德圆满
  453. grPhoto.Dispose();
  454.  
  455. //将grPhoto保存
  456. imgPhoto.Save(targetImage, ImageFormat.Jpeg);
  457. imgPhoto.Dispose();
  458.  
  459. return targetImage.Replace(PicturePath, "");
  460. }
  461. }
  462.  
  463. /// <summary>
  464. /// 装载水印图片的相关信息
  465. /// </summary>
  466. public class WaterImage
  467. {
  468. public WaterImage ()
  469. {
  470.  
  471. }
  472.  
  473. private string m_sourcePicture;
  474. /// <summary>
  475. /// 源图片地址名字(带后缀)
  476. /// </summary>
  477. public string SourcePicture
  478. {
  479. get { return m_sourcePicture; }
  480. set { m_sourcePicture = value; }
  481. }
  482.  
  483. private string m_waterImager;
  484. /// <summary>
  485. /// 水印图片名字(带后缀)
  486. /// </summary>
  487. public string WaterPicture
  488. {
  489. get { return m_waterImager; }
  490. set { m_waterImager = value; }
  491. }
  492.  
  493. private float m_alpha;
  494. /// <summary>
  495. /// 水印图片文字的透明度
  496. /// </summary>
  497. public float Alpha
  498. {
  499. get { return m_alpha; }
  500. set { m_alpha = value; }
  501. }
  502.  
  503. private ImagePosition m_postition;
  504. /// <summary>
  505. /// 水印图片或文字在图片中的位置
  506. /// </summary>
  507. public ImagePosition Position
  508. {
  509. get { return m_postition; }
  510. set { m_postition = value; }
  511. }
  512.  
  513. private string m_words;
  514. /// <summary>
  515. /// 水印文字的内容
  516. /// </summary>
  517. public string Words
  518. {
  519. get { return m_words; }
  520. set { m_words = value; }
  521. }
  522.  
  523. }

ASP.NET(C#)图片加文字、图片水印,神啊,看看吧的更多相关文章

  1. PHP给图片加文字(水印)

    准备工作: 代码: <?php header("Content-type: image/jpeg"); //浏览器输出,如不需要可去掉此行 $im = @imagecreat ...

  2. PHP给图片加文字水印

    <?php /*给图片加文字水印的方法*/ $dst_path = 'http://f4.topitme.com/4/15/11/1166351597fe111154l.jpg'; $dst = ...

  3. Java图片加文字水印

    Java图片加文字水印 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.I ...

  4. C#给图片加文字水印

    public class TxtWaterMark { public enum WaterPositionMode { LeftTop,//左上 LeftBottom,//左下 RightTop,// ...

  5. C#给图片加文字和图片的水印

    /// <summary> /// WaterMark 的摘要说明 /// </summary> /// 图片加水印 /// <param name="strC ...

  6. Android给图片加文字和图片水印

    我们在做项目的时候有时候需要给图片添加水印,水寒今天就遇到了这样的问题,所以搞了一个工具类,贴出来大家直接调用就行. /** * 图片工具类 * @author 水寒 * 欢迎访问水寒的个人博客:ht ...

  7. ios图片添加文字或者水印

    在项目中,我们会对图片做一些处理,但是我们要记住,一般在客户端做图片处理的数量不宜太多,因为受设备性能的限制,如果批量的处理图片,将会带来交互体验性上的一些问题.首先让我们来看看在图片上添加文字的方法 ...

  8. 安卓使用TextView实现图片加文字说明

    背景:通讯录列表,每个单元格显示头像+名字,且头像显示圆形 方案一:ImageView + TextView 方案二:只用TextView + drawableLeft 属性 <TextView ...

  9. php给图片加文字

    在图片上加文字是论坛,博客,新闻网站上最喜欢用的功能,防止盗图.这里看看代码是如何实现的. 首先还是upload_image.php这个文件,注意这里的caption文本框中输入的内容最终会写到图片上 ...

随机推荐

  1. 2019/7/18----2.1.tomcat启动报错问题

    问题描述:java.lang.UnsupportedClassVersionError: filters/SetCharacterEncodingFilter : Unsupported major. ...

  2. HTTP文件上传

    看到网上很多链接文件(word.pdf...)可以下载,想制作http下载链接. 其实是将某文件直接放在服务器上搭建的网站上某目录下即可,例如:http://xxx:port/UpgradePack/ ...

  3. 转 Storm JAVA_HOME is incorrectly set.

    问题可能有两个原因: 1.在环境变量中未设置JAVA_HOME变量名称. 解决办法: 在环境变量中添加. 或者在storm中的bin文件下有一个storm-config.cmd,使用文本打开,查询JA ...

  4. PHP 高手博客网站集合

    风雪之隅-Laruence的博客 韩天峰(Rango)的博客 我的志愿是做一个校长 张宴的博客 - Web系统架构与底层研发 沈逸的个人站点 博学无忧 - 信海龙的博客

  5. Ubuntu安装php7.0环境

    1.下载必须组件 sudo apt-get install libxml2-dev sudo apt-get install curl 参考文献:http://php.net/manual/zh/in ...

  6. 繁繁的数字 背包DP

    繁繁的数字 背包DP 问一个数\(n\)有多少种二进制分解方案数 \(n\le 10^5\) 如7有7=4+2+1=4+1+1+1=2+2+2+1=2+2+1+1+1=2+1+1+1+1+1=1+1+ ...

  7. codevs 4014EZ系列

    4014 EZ系列之丁畅大大打小怪兽   题目描述 Description 丁畅大大除了喜欢吃鸡腿和番茄炒鸡蛋,还喜欢变成奥特曼去打小怪兽. 有一天,他遇到了迪迦!!!!!!!!!!!!!!!!!!! ...

  8. P1902 刺杀大使

    题目描述 伊朗伊斯兰革命卫队(某恐怖组织)正在策划一起刺杀行动,他们的目标是沙特驻美大 使朱拜尔.他们来到了沙特驻美使馆,准备完成此次刺杀,要进入使馆首先必须通过使馆前 的防御迷阵. 迷阵由 n*m ...

  9. Java SpringBoot全局错误处理类,返回标准结果

    package demo.utils; import com.alibaba.fastjson.JSON; import demo.controller.ProductController; impo ...

  10. linux 命令scp

    scp命令网络传输文件 上传文件 scp 文件名 usename@10.233.23.100:Data/ 上传文件夹到服务器 scp -r 文件夹(不带/)usename@10.233.23.100: ...