c# asp.net 给一个图片打上水印,并且上传

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