1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Drawing.Imaging;
  13. /// <summary>
  14. ///ImgWater 的摘要说明
  15. /// </summary>
  16. public class ImgWater
  17. {
  18. public ImgWater()
  19. {
  20. //
  21. //TODO: 在此处添加构造函数逻辑
  22. //
  23. }
  24. /// <summary>
  25. /// 图片水印
  26. /// </summary>
  27. /// <param name="ImgFile">原图文件地址</param>
  28. /// <param name="WaterImg">水印图片地址</param>
  29. /// <param name="sImgPath">水印图片保存地址</param>
  30. /// <param name="Alpha">水印透明度设置</param>
  31. /// <param name="iScale">水印图片在原图上的显示比例</param>
  32. /// <param name="intDistance">水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效</param>
  33. public bool zzsImgWater(string ImgFile,string WaterImg,string sImgPath,
  34. float Alpha, float iScale,int intDistance)
  35. {
  36. try
  37. {
  38. //装载图片
  39. FileStream fs = new FileStream(ImgFile, FileMode.Open);
  40. BinaryReader br = new BinaryReader(fs);
  41. byte[] bytes = br.ReadBytes((int)fs.Length);
  42. br.Close();
  43. fs.Close();
  44. MemoryStream ms = new MemoryStream(bytes);
  45. System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
  46. //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
  47. int imgPhotoWidth = imgPhoto.Width;
  48. int imgPhotoHeight = imgPhoto.Height;
  49. System.Drawing.Image imgWatermark = new Bitmap(WaterImg);
  50. int imgWatermarkWidth = imgWatermark.Width;
  51. int imgWatermarkHeight = imgWatermark.Height;
  52. //计算水印图片尺寸
  53. decimal aScale = Convert.ToDecimal(iScale);
  54. decimal pScale = 0.05M;
  55. decimal MinScale = aScale - pScale;
  56. decimal MaxScale = aScale + pScale;
  57. int imgWatermarkWidthNew = imgWatermarkWidth;
  58. int imgWatermarkHeightNew = imgWatermarkHeight;
  59. if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth >= imgPhotoHeight)
  60. if (imgWatermarkWidth > imgWatermarkHeight)
  61. )) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), ) <= MaxScale))
  62. {
  63. }
  64. else
  65. {
  66. imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
  67. imgWatermarkHeightNew = Convert.ToInt32((imgPhotoWidth * aScale / imgWatermarkWidth) * imgWatermarkHeight);
  68. }
  69. else
  70. )) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), ) <= MaxScale))
  71. {
  72. }
  73. else
  74. {
  75. imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
  76. imgWatermarkWidthNew = Convert.ToInt32((imgPhotoHeight * aScale / imgWatermarkHeight) * imgWatermarkWidth);
  77. }
  78. if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth >= imgWatermarkHeight)
  79. {
  80. imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
  81. imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
  82. }
  83. if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
  84. {
  85. imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
  86. imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
  87. }
  88. if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
  89. {
  90. imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
  91. imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
  92. }
  93. if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth <= imgPhotoHeight)
  94. if (imgWatermarkWidth > imgWatermarkHeight)
  95. )) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), ) <= MaxScale))
  96. {
  97. }
  98. else
  99. {
  100. imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
  101. imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
  102. }
  103. else
  104. )) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), ) <= MaxScale))
  105. {
  106. }
  107. else
  108. {
  109. imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
  110. imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
  111. }
  112. if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth <= imgWatermarkHeight)
  113. {
  114. imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
  115. imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
  116. }
  117. if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
  118. {
  119. imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
  120. imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
  121. }
  122. if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
  123. {
  124. imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
  125. imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
  126. }
  127. //将原图画出来
  128. Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  129. bmPhoto.SetResolution(, );
  130. Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
  131. gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  132. gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  133. gbmPhoto.Clear(Color.White);
  134. gbmPhoto.DrawImage(
  135. imgPhoto
  136. , , , imgPhotoWidth, imgPhotoHeight)
  137. ,
  138. ,
  139. , imgPhotoWidth
  140. , imgPhotoHeight
  141. , GraphicsUnit.Pixel
  142. );
  143. Bitmap bmWatermark = new Bitmap(bmPhoto);
  144. bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
  145. Graphics gWatermark = Graphics.FromImage(bmWatermark);
  146. //指定高质量显示水印图片质量
  147. gWatermark.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  148. gWatermark.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  149. ImageAttributes imageAttributes = new ImageAttributes();
  150. //设置两种颜色,达到合成效果
  151. ColorMap colorMap = new ColorMap();
  152. colorMap.OldColor = Color.FromArgb(, , , );
  153. colorMap.NewColor = Color.FromArgb(, , , );
  154. ColorMap[] remapTable = { colorMap };
  155. imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap);
  156. //用矩阵设置水印图片透明度
  157. float[][] colorMatrixElements = {
  158. new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
  159. new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
  160. new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
  161. new float[] {0.0f, 0.0f, 0.0f, Alpha, 0.0f},
  162. new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
  163. };
  164. System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);
  165. imageAttributes.SetColorMatrix(wmColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);
  166. //确定水印边距
  167. int xPos = imgPhotoWidth - imgWatermarkWidthNew;
  168. int yPos = imgPhotoHeight - imgWatermarkHeightNew;
  169. ;
  170. ;
  171. if (xPos > intDistance)
  172. xPosOfWm = xPos - intDistance;
  173. else
  174. xPosOfWm = xPos;
  175. if (yPos > intDistance)
  176. yPosOfWm = yPos - intDistance;
  177. else
  178. yPosOfWm = yPos;
  179. gWatermark.DrawImage(
  180. imgWatermark
  181. , new Rectangle(xPosOfWm, yPosOfWm, imgWatermarkWidthNew, imgWatermarkHeightNew)
  182. ,
  183. ,
  184. , imgWatermarkWidth
  185. , imgWatermarkHeight
  186. , GraphicsUnit.Pixel
  187. , imageAttributes
  188. );
  189. imgPhoto = bmWatermark;
  190. //以jpg格式保存图片
  191. imgPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  192. //销毁对象
  193. gbmPhoto.Dispose();
  194. gWatermark.Dispose();
  195. imgPhoto.Dispose();
  196. imgWatermark.Dispose();
  197. return true;
  198. }
  199. catch
  200. {
  201. return false;
  202. }
  203. }
  204. /**//// <summary>
  205. /// 文字水印
  206. /// </summary>
  207. /// <param name="ImgFile">原图文件地址</param>
  208. /// <param name="TextFont">水印文字</param>
  209. /// <param name="sImgPath">文字水印图片保存地址</param>
  210. /// <param name="hScale">高度位置</param>
  211. /// <param name="widthFont">文字块在图片中所占宽度比例</param>
  212. /// <param name="Alpha">文字透明度 其数值的范围在0到255</param>
  213. public bool zzsTextWater(
  214. string ImgFile
  215. , string TextFont
  216. , string sImgPath
  217. , float hScale
  218. , float widthFont
  219. , int Alpha
  220. )
  221. {
  222. try
  223. {
  224. FileStream fs = new FileStream(ImgFile, FileMode.Open);
  225. BinaryReader br = new BinaryReader(fs);
  226. byte[] bytes = br.ReadBytes((int)fs.Length);
  227. br.Close();
  228. fs.Close();
  229. MemoryStream ms = new MemoryStream(bytes);
  230. System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
  231. //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
  232. int imgPhotoWidth = imgPhoto.Width;
  233. int imgPhotoHeight = imgPhoto.Height;
  234. Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  235. bmPhoto.SetResolution(, );
  236. Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
  237. gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  238. gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  239. gbmPhoto.DrawImage(
  240. imgPhoto
  241. , , , imgPhotoWidth, imgPhotoHeight)
  242. ,
  243. ,
  244. , imgPhotoWidth
  245. , imgPhotoHeight
  246. , GraphicsUnit.Pixel
  247. );
  248. //建立字体大小的数组,循环找出适合图片的水印字体
  249. //int[] sizes = new int[] { 1000, 800, 700, 650, 600, 560, 540, 500, 450, 400, 380, 360, 340, 320, 300, 280, 260, 240, 220, 200, 180, 160, 140, 120, 100, 80, 72, 64, 48, 32, 28, 26, 24, 20, 28, 16, 14, 12, 10, 8, 6, 4, 2 };
  250. , , , , , , };
  251. System.Drawing.Font crFont = null;
  252. System.Drawing.SizeF crSize = new SizeF();
  253. ; i < ; i++)
  254. {
  255. crFont = new Font("微软雅黑", sizes[i], FontStyle.Bold);
  256. crSize = gbmPhoto.MeasureString(TextFont, crFont);
  257. if ((ushort)crSize.Width < (ushort)imgPhotoWidth * widthFont)
  258. break;
  259. }
  260. //设置水印字体的位置
  261. //int yPixlesFromBottom = (int)(imgPhotoHeight * hScale);
  262. //float yPosFromBottom = ((imgPhotoHeight - yPixlesFromBottom) - (crSize.Height / 2));
  263. //float xCenterOfImg = (imgPhotoWidth * 1 / 2);
  264. float yPosFromBottom = imgPhotoHeight * 0.85f;
  265. float xCenterOfImg = imgPhotoWidth * 0.8f;
  266. //if (xCenterOfImg<crSize.Height)
  267. // xCenterOfImg = (imgPhotoWidth * (1 - (crSize.Height)/ imgPhotoWidth));
  268. System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat();
  269. StrFormat.Alignment = System.Drawing.StringAlignment.Center;
  270. //
  271. System.Drawing.SolidBrush semiTransBrush2 = , , ));
  272. gbmPhoto.DrawString(
  273. TextFont
  274. , crFont
  275. , semiTransBrush2
  276. , , yPosFromBottom + )
  277. , StrFormat
  278. );
  279. System.Drawing.SolidBrush semiTransBrush = , , ));
  280. //gbmPhoto.FillRectangle(semiTransBrush2, new RectangleF(new PointF(xCenterOfImg - crSize.Width / 2, yPosFromBottom - 4), crSize));
  281. gbmPhoto.DrawString(
  282. TextFont
  283. , crFont
  284. , semiTransBrush
  285. , new System.Drawing.PointF(xCenterOfImg, yPosFromBottom)
  286. , StrFormat
  287. );
  288. bmPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  289. gbmPhoto.Dispose();
  290. imgPhoto.Dispose();
  291. bmPhoto.Dispose();
  292. return true;
  293. }
  294. catch(Exception ex)
  295. {
  296. return false;
  297. }
  298. }
  299. /**//// <summary>
  300. /// 文字和Logo图片水印
  301. /// </summary>
  302. /// <param name="ImgFile">原图文件地址</param>
  303. /// <param name="WaterImg">水印图片地址</param>
  304. /// <param name="TextFont">水印文字信息</param>
  305. /// <param name="sImgPath">生存水印图片后的保存地址</param>
  306. /// <param name="ImgAlpha">水印图片的透明度</param>
  307. /// <param name="imgiScale">水印图片在原图上的显示比例</param>
  308. /// <param name="intimgDistance">水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效</param>
  309. /// <param name="texthScale">水印文字高度位置,从图片底部开始计算,0-1</param>
  310. /// <param name="textwidthFont">文字块在图片中所占宽度比例 0-1</param>
  311. /// <param name="textAlpha">文字透明度 其数值的范围在0到255</param>
  312. public void zzsImgTextWater(
  313. string ImgFile
  314. , string WaterImg
  315. , string TextFont
  316. , string sImgPath
  317. , float ImgAlpha
  318. , float imgiScale
  319. , int intimgDistance
  320. , float texthScale
  321. , float textwidthFont
  322. , int textAlpha
  323. )
  324. {
  325. try
  326. {
  327. FileStream fs = new FileStream(ImgFile, FileMode.Open);
  328. BinaryReader br = new BinaryReader(fs);
  329. byte[] bytes = br.ReadBytes((int)fs.Length);
  330. br.Close();
  331. fs.Close();
  332. MemoryStream ms = new MemoryStream(bytes);
  333. System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
  334. //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
  335. int imgPhotoWidth = imgPhoto.Width;
  336. int imgPhotoHeight = imgPhoto.Height;
  337. Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  338. bmPhoto.SetResolution(, );
  339. Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
  340. gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  341. gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  342. gbmPhoto.DrawImage(
  343. imgPhoto
  344. , , , imgPhotoWidth, imgPhotoHeight)
  345. ,
  346. ,
  347. , imgPhotoWidth
  348. , imgPhotoHeight
  349. , GraphicsUnit.Pixel
  350. );
  351. //建立字体大小的数组,循环找出适合图片的水印字体
  352. , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , };
  353. System.Drawing.Font crFont = null;
  354. System.Drawing.SizeF crSize = new SizeF();
  355. ; i < ; i++)
  356. {
  357. crFont = new Font("arial", sizes[i], FontStyle.Bold);
  358. crSize = gbmPhoto.MeasureString(TextFont, crFont);
  359. if ((ushort)crSize.Width < (ushort)imgPhotoWidth * textwidthFont)
  360. break;
  361. }
  362. //设置水印字体的位置
  363. int yPixlesFromBottom = (int)(imgPhotoHeight * texthScale);
  364. ));
  365. / );
  366. System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat();
  367. StrFormat.Alignment = System.Drawing.StringAlignment.Center;
  368. //
  369. System.Drawing.SolidBrush semiTransBrush2 = , , ));
  370. gbmPhoto.DrawString(
  371. TextFont
  372. , crFont
  373. , semiTransBrush2
  374. , , yPosFromBottom + )
  375. , StrFormat
  376. );
  377. System.Drawing.SolidBrush semiTransBrush = , , ));
  378. gbmPhoto.DrawString(
  379. TextFont
  380. , crFont
  381. , semiTransBrush
  382. , new System.Drawing.PointF(xCenterOfImg, yPosFromBottom)
  383. , StrFormat
  384. );
  385. System.Drawing.Image imgWatermark = new Bitmap(WaterImg);
  386. int imgWatermarkWidth = imgWatermark.Width;
  387. int imgWatermarkHeight = imgWatermark.Height;
  388. //计算水印图片尺寸
  389. decimal aScale = Convert.ToDecimal(imgiScale);
  390. decimal pScale = 0.05M;
  391. decimal MinScale = aScale - pScale;
  392. decimal MaxScale = aScale + pScale;
  393. int imgWatermarkWidthNew = imgWatermarkWidth;
  394. int imgWatermarkHeightNew = imgWatermarkHeight;
  395. if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth >= imgPhotoHeight)
  396. if (imgWatermarkWidth > imgWatermarkHeight)
  397. )) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), ) <= MaxScale))
  398. {
  399. }
  400. else
  401. {
  402. imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
  403. imgWatermarkHeightNew = Convert.ToInt32((imgPhotoWidth * aScale / imgWatermarkWidth) * imgWatermarkHeight);
  404. }
  405. else
  406. )) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), ) <= MaxScale))
  407. {
  408. }
  409. else
  410. {
  411. imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
  412. imgWatermarkWidthNew = Convert.ToInt32((imgPhotoHeight * aScale / imgWatermarkHeight) * imgWatermarkWidth);
  413. }
  414. if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth >= imgWatermarkHeight)
  415. {
  416. imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
  417. imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
  418. }
  419. if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
  420. {
  421. imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
  422. imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
  423. }
  424. if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
  425. {
  426. imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
  427. imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
  428. }
  429. if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth <= imgPhotoHeight)
  430. if (imgWatermarkWidth > imgWatermarkHeight)
  431. )) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), ) <= MaxScale))
  432. {
  433. }
  434. else
  435. {
  436. imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
  437. imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
  438. }
  439. else
  440. )) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), ) <= MaxScale))
  441. {
  442. }
  443. else
  444. {
  445. imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
  446. imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
  447. }
  448. if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth <= imgWatermarkHeight)
  449. {
  450. imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
  451. imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
  452. }
  453. if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
  454. {
  455. imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
  456. imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
  457. }
  458. if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
  459. {
  460. imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
  461. imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
  462. }
  463. //将原图画出来
  464. Bitmap bmWatermark = new Bitmap(bmPhoto);
  465. bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
  466. Graphics gWatermark = Graphics.FromImage(bmWatermark);
  467. //指定高质量显示水印图片质量
  468. gWatermark.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  469. gWatermark.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  470. System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes();
  471. //设置两种颜色,达到合成效果
  472. System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap();
  473. colorMap.OldColor = Color.FromArgb(, , , );
  474. colorMap.NewColor = Color.FromArgb(, , , );
  475. System.Drawing.Imaging.ColorMap[] remapTable = { colorMap };
  476. imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap);
  477. //用矩阵设置水印图片透明度
  478. float[][] colorMatrixElements = {
  479. new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
  480. new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
  481. new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
  482. new float[] {0.0f, 0.0f, 0.0f, ImgAlpha, 0.0f},
  483. new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
  484. };
  485. System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);
  486. imageAttributes.SetColorMatrix(wmColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);
  487. //确定水印边距
  488. int xPos = imgPhotoWidth - imgWatermarkWidthNew;
  489. int yPos = imgPhotoHeight - imgWatermarkHeightNew;
  490. ;
  491. ;
  492. if (xPos > intimgDistance)
  493. xPosOfWm = xPos - intimgDistance;
  494. else
  495. xPosOfWm = xPos;
  496. if (yPos > intimgDistance)
  497. yPosOfWm = yPos - intimgDistance;
  498. else
  499. yPosOfWm = yPos;
  500. gWatermark.DrawImage(
  501. imgWatermark
  502. , new Rectangle(xPosOfWm, yPosOfWm, imgWatermarkWidthNew, imgWatermarkHeightNew)
  503. ,
  504. ,
  505. , imgWatermarkWidth
  506. , imgWatermarkHeight
  507. , GraphicsUnit.Pixel
  508. , imageAttributes
  509. );
  510. imgPhoto = bmWatermark;
  511. //以jpg格式保存图片
  512. imgPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  513. //销毁对象
  514. gbmPhoto.Dispose();
  515. gWatermark.Dispose();
  516. bmPhoto.Dispose();
  517. imgPhoto.Dispose();
  518. imgWatermark.Dispose();
  519. }
  520. catch
  521. {
  522. }
  523. }
  524. /**//// <summary>
  525. /// 缩略图
  526. /// </summary>
  527. /// <param name="ImgFile">原图文件地址</param>
  528. /// <param name="sImgPath">缩略图保存地址</param>
  529. /// <param name="ResizeWidth">缩略图宽度</param>
  530. /// <param name="ResizeHeight">缩略图高度</param>
  531. /// <param name="BgColor">缩略图背景颜色,注意,背景颜色只能指定KnownColor中的值,如blue,red,green等</param>
  532. public bool zzsResizeImg(string ImgFile, string sImgPath, int ResizeWidth, int ResizeHeight, string BgColor)
  533. {
  534. try
  535. {
  536. FileStream fs = new FileStream(ImgFile, FileMode.Open);
  537. BinaryReader br = new BinaryReader(fs);
  538. byte[] bytes = br.ReadBytes((int)fs.Length);
  539. br.Close();
  540. fs.Close();
  541. MemoryStream ms = new MemoryStream(bytes);
  542. System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
  543. //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
  544. int imgPhotoWidth = imgPhoto.Width;
  545. int imgPhotoHeight = imgPhoto.Height;
  546. ;
  547. ;
  548. ;
  549. ;
  550. if (imgPhotoWidth >= ResizeWidth && imgPhotoHeight >= ResizeHeight)
  551. {
  552. NewWidth = ResizeWidth;
  553. NewHeight = Convert.ToInt32(imgPhotoHeight * Math.Round(Convert.ToDecimal(ResizeWidth) / Convert.ToDecimal(imgPhotoWidth), ));
  554. startX = ;
  555. StartY = (ResizeHeight - NewHeight) / ;
  556. }
  557. if (ResizeWidth > imgPhotoWidth && ResizeHeight < imgPhotoHeight)
  558. {
  559. NewHeight = ResizeHeight;
  560. NewWidth = Convert.ToInt32(imgPhotoWidth * Math.Round(Convert.ToDecimal(ResizeHeight) / Convert.ToDecimal(imgPhotoHeight), ));
  561. startX = (ResizeWidth - NewWidth) / ;
  562. StartY = ;
  563. }
  564. if (ResizeWidth < imgPhotoWidth && ResizeHeight > imgPhotoHeight)
  565. {
  566. NewWidth = ResizeWidth;
  567. NewHeight = Convert.ToInt32(imgPhotoHeight * Math.Round(Convert.ToDecimal(ResizeWidth) / Convert.ToDecimal(imgPhotoWidth), ));
  568. startX = ;
  569. StartY = (ResizeHeight - NewHeight) / ;
  570. }
  571. if (imgPhotoWidth < ResizeWidth && imgPhotoHeight < ResizeHeight)
  572. {
  573. NewWidth = imgPhotoWidth;
  574. NewHeight = imgPhotoHeight;
  575. startX = (ResizeWidth - imgPhotoWidth) / ;
  576. StartY = (ResizeHeight - imgPhotoHeight) / ;
  577. }
  578. //计算缩放图片尺寸
  579. Bitmap bmPhoto = new Bitmap(ResizeWidth, ResizeHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  580. bmPhoto.SetResolution(, );
  581. Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
  582. gbmPhoto.Clear(Color.FromName(BgColor));
  583. gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  584. gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  585. gbmPhoto.DrawImage(
  586. imgPhoto
  587. , new Rectangle(startX, StartY, NewWidth, NewHeight)
  588. , , , imgPhotoWidth, imgPhotoHeight)
  589. , GraphicsUnit.Pixel
  590. );
  591. bmPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  592. imgPhoto.Dispose();
  593. gbmPhoto.Dispose();
  594. bmPhoto.Dispose();
  595. ms.Close();
  596. return true;
  597. }
  598. catch
  599. {
  600. return false;
  601. }
  602. }
  603. /**//// <summary>
  604. /// 图片剪切
  605. /// </summary>
  606. /// <param name="ImgFile">原图文件地址</param>
  607. /// <param name="sImgPath">缩略图保存地址</param>
  608. /// <param name="PointX">剪切起始点 X坐标</param>
  609. /// <param name="PointY">剪切起始点 Y坐标</param>
  610. /// <param name="CutWidth">剪切宽度</param>
  611. /// <param name="CutHeight">剪切高度</param>
  612. public bool zzsCutImg(string ImgFile, string sImgPath, int PointX, int PointY, int CutWidth, int CutHeight)
  613. {
  614. FileStream fs = new FileStream(ImgFile, FileMode.Open);
  615. BinaryReader br = new BinaryReader(fs);
  616. try
  617. {
  618. byte[] bytes = br.ReadBytes((int)fs.Length);
  619. br.Close();
  620. fs.Close();
  621. MemoryStream ms = new MemoryStream(bytes);
  622. System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
  623. //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
  624. //此处只能用filestream,用 System.Drawing.Image则会报多过进程访问文件的错误,会锁定文件
  625. Bitmap bmPhoto = new Bitmap(CutWidth, CutHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  626. bmPhoto.SetResolution(, );
  627. Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
  628. gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  629. gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  630. gbmPhoto.DrawImage(
  631. imgPhoto
  632. , , , CutWidth, CutHeight)
  633. , new Rectangle(PointX, PointY, CutHeight, CutHeight)
  634. , GraphicsUnit.Pixel
  635. );
  636. bmPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  637. imgPhoto.Dispose();
  638. gbmPhoto.Dispose();
  639. bmPhoto.Dispose();
  640. ms.Close();
  641. return true;
  642. }
  643. catch
  644. {
  645. return false;
  646. }
  647. finally
  648. {
  649. }
  650. }
  651. }

public partial class WebForm1 : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    ImgWater img = new ImgWater();
    bool f = img.zzsTextWater(@"F:\ConsoleApplication1\LOGO.png",
    "水印测试", @"F:\LOGO.png",15.00F,0.25F,100);

  1.  

  }
}

  1.  

C#添加水印的更多相关文章

  1. PHP-生成缩略图和添加水印图-学习笔记

    1.开始 在网站上传图片过程,经常用到缩略图功能.这里我自己写了一个图片处理的Image类,能生成缩略图,并且可以添加水印图. 2.如何生成缩略图 生成缩略图,关键的是如何计算缩放比率. 这里,我根据 ...

  2. C#中如何给Excel添加水印

    我们知道Microsoft Excel并没有内置的功能直接给Excel表添加水印,但是其实我们可以用其他变通的方式来解决此问题,如通过添加页眉图片或艺术字的方法来模仿水印的外观.所以在这篇文章中,我将 ...

  3. C# 给word文档添加水印

    和PDF一样,在word中,水印也分为图片水印和文本水印,给文档添加图片水印可以使文档变得更为美观,更具有吸引力.文本水印则可以保护文档,提醒别人该文档是受版权保护的,不能随意抄袭.前面我分享了如何给 ...

  4. C# 给PDF文件添加水印

      水印种类及功能介绍 PDF水印分为两种:文本水印和图片水印.文本水印一般被用在商业领域,提醒读者该文档是受版权保护的,其他人不能抄袭或者免费使用.除了这个特征,水印还可以用来标记这个文档 的一些基 ...

  5. C#基于两种需求向图片添加水印

    使用场景 1.也就是大家经常用的,一般是图片的4个角落,基于横纵坐标来添加. 2.在图片内基于固定位置,文字始终居中.刚开始我基于第一种场景来根据水印汉字的长度来计算坐标,后来发现方法始终不可靠.现在 ...

  6. PHP 图片处理工具类(添加水印与生成缩略图)

    =================ImageTool.class.php================= <?php class ImageTool { private $imagePath; ...

  7. ios 给图片添加水印

    //第一种添加水印方法 -(UIImage *)watermarkImage:(UIImage *)img withName:(NSString *)name{ NSString* mark = na ...

  8. Android 图片添加水印图片或者文字

    给图片添加水印的基本思路都是载入原图,添加文字或者载入水印图片,保存图片这三个部分 添加水印图片: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

  9. iOS 学习 - 15.添加水印

    绘制到位图 下面利用位图图形上下文给一个图片添加水印,在下面的程序中我们首先创建上下文,然后在上下文中绘制图片.直线和文本,最后从当前位图上下文中取得最终形成的新图片显示到界面 - (void)vie ...

  10. c#图片添加水印

    今天讲一个上传图片添加水印的方法,直接上代码吧 protected void Button1_Click(object sender, EventArgs e)    {        int loc ...

随机推荐

  1. vim-进入插入模式快捷键

    vim中有一些命令,是同时包含有大小写两种的.现在就集中测试下他们的区别:     1.A 跟a A-光标所在行的末尾插入 a-光标后插入 2.I 跟i I-光标所在行的非空字符前插入 i-光标前位置 ...

  2. HDP和HDF

    参考文档: HDP安装: 官方文档:https://docs.hortonworks.com/HDPDocuments/Ambari-2.5.0.3/bk_ambari-installation/co ...

  3. jQuery插件--根据数据加载的进度动画案例

    css: *{ margin:; padding:; } @media screen and (min-width:320px){ html{font-size:12px;}} @media scre ...

  4. (JavaScript基础向)日常小发现:forEach等函数的第二个参数的用法

    forEach函数用得平时用得比较多,但是从来没想到forEach函数还有第二个参数. 这里是菜鸟教程对forEach函数的详细说明:forEach的详细说明. 如上图,forEach函数有第二个参数 ...

  5. LuoguP1251 餐巾计划问题(费用流)

    题目描述 一个餐厅在相继的 NN 天里,每天需用的餐巾数不尽相同.假设第 ii 天需要 r_iri​块餐巾( i=1,2,...,N).餐厅可以购买新的餐巾,每块餐巾的费用为 pp 分;或者把旧餐巾送 ...

  6. 小米开源文件管理器MiCodeFileExplorer-源码研究(6)-媒体文件MediaFile和文件类型MimeUtils

    接着之前的第4篇,本篇的2个类,仍然是工具类.MediaFile,媒体文件,定义了一大堆的常量,真正的有用的方法就几个.isAudioFileType.isVideoFileType之类的. Mime ...

  7. 微信小程序从零开始开发步骤(七)引入外部js 文件

    上一章讲到小程序页面的四种常见的跳转的方法,这一章写如何引入一个外部的js文件,既utils文件夹的用处,其实步骤很简单: 1:准备好外部想要引入的外部文件,命名为util.js,并且填充固定的文件内 ...

  8. HDU——T 1573 X问题

    http://acm.hdu.edu.cn/showproblem.php?pid=1573 Time Limit: 1000/1000 MS (Java/Others)    Memory Limi ...

  9. 32款iOS开发插件和工具介绍[效率]

    插件和工具介绍内容均收集于网络,太多了就不一一注明了,在此谢过!   1.Charles 为了调试与server端的网络通讯协议.经常须要截取网络封包来分析. Charles通过将自己设置成系统的网络 ...

  10. BZOJ3277: 串(后缀自动机,Parent树,Dfs序)

    Description 字符串是oi界常考的问题.现在给定你n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中 至少k个字符串的子串(注意包括本身). Input 第一行两个整数n, ...