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

转载于:https://www.cnblogs.com/jordan2009/archive/2009/08/25/1553598.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值