自动生成图片缩略图、加水印、远程抓取上传

下面的方法,.NET1.1和2.0都可以使用

所要用到的命名空间

c# 代码
 
  1. using System.IO; 
  2. using System.Net; 
  3. using System.Text.RegularExpressions;

c# 代码
 
  1. /**/
  2. /// <summary>
  3. /// 生成缩略图
  4. /// </summary>
  5. /// <param name="originalImagePath">源图路径(物理路径)</param>
  6. /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
  7. /// <param name="width">缩略图宽度</param>
  8. /// <param name="height">缩略图高度</param>
  9. /// <param name="mode">生成缩略图的方式</param>   
  10. public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode) 
  11.     System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); 
  12.     int towidth = width; 
  13.     int toheight = height; 
  14.     int x = 0; 
  15.     int y = 0; 
  16.     int ow = originalImage.Width; 
  17.     int oh = originalImage.Height; 
  18.     switch (mode) 
  19.     { 
  20.         case "HW"://指定高宽缩放(可能变形)               
  21.             break
  22.         case "W"://指定宽,高按比例                   
  23.             toheight = originalImage.Height * width / originalImage.Width; 
  24.             break
  25.         case "H"://指定高,宽按比例
  26.             towidth = originalImage.Width * height / originalImage.Height; 
  27.             break
  28.         case "Cut"://指定高宽裁减(不变形)               
  29.             if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) 
  30.             { 
  31.                 oh = originalImage.Height; 
  32.                 ow = originalImage.Height * towidth / toheight; 
  33.                 y = 0; 
  34.                 x = (originalImage.Width - ow) / 2; 
  35.             } 
  36.             else
  37.             { 
  38.                 ow = originalImage.Width; 
  39.                 oh = originalImage.Width * height / towidth; 
  40.                 x = 0; 
  41.                 y = (originalImage.Height - oh) / 2; 
  42.             } 
  43.             break
  44.         default
  45.             break
  46.     } 
  47.     //新建一个bmp图片
  48.     System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); 
  49.     //新建一个画板
  50.     System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); 
  51.     //设置高质量插值法
  52.     g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 
  53.     //设置高质量,低速度呈现平滑程度
  54.     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
  55.     //清空画布并以透明背景色填充
  56.     g.Clear(System.Drawing.Color.Transparent); 
  57.     //在指定位置并且按指定大小绘制原图片的指定部分
  58.     g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), 
  59.         new System.Drawing.Rectangle(x, y, ow, oh), 
  60.         System.Drawing.GraphicsUnit.Pixel); 
  61.     try
  62.     { 
  63.         //以jpg格式保存缩略图
  64.         bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); 
  65.     } 
  66.     catch (System.Exception e) 
  67.     { 
  68.         throw e; 
  69.     } 
  70.     finally
  71.     { 
  72.         originalImage.Dispose(); 
  73.         bitmap.Dispose(); 
  74.         g.Dispose(); 
  75.     } 
  76. /**/
  77. /// <summary>
  78. /// 在图片上增加文字水印
  79. /// </summary>
  80. /// <param name="Path">原服务器图片路径</param>
  81. /// <param name="Path_sy">生成的带文字水印的图片路径</param>
  82. protected void AddWater(string Path, string Path_sy) 
  83.     string addText = "51aspx.com"
  84.     System.Drawing.Image image = System.Drawing.Image.FromFile(Path); 
  85.     System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image); 
  86.     g.DrawImage(image, 0, 0, image.Width, image.Height); 
  87.     System.Drawing.Font f = new System.Drawing.Font("Verdana", 60); 
  88.     System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green); 
  89.     g.DrawString(addText, f, b, 35, 35); 
  90.     g.Dispose(); 
  91.     image.Save(Path_sy); 
  92.     image.Dispose(); 
  93. /**/
  94. /// <summary>
  95. /// 在图片上生成图片水印
  96. /// </summary>
  97. /// <param name="Path">原服务器图片路径</param>
  98. /// <param name="Path_syp">生成的带图片水印的图片路径</param>
  99. /// <param name="Path_sypf">水印图片路径</param>
  100. protected void AddWaterPic(string Path, string Path_syp, string Path_sypf) 
  101.     System.Drawing.Image image = System.Drawing.Image.FromFile(Path); 
  102.     System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf); 
  103.     System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image); 
  104.     g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel); 
  105.     g.Dispose(); 
  106.     image.Save(Path_syp); 
  107.     image.Dispose(); 

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

接下来的是远程抓取并上传,我把他和FCK编辑器结合使用,原理是FCK编辑器获取了内容(一般合着远程的图片),然后在经过方法的过滤(图片),然后把图片上传到本地服务器
,然后通过Replace替换FCK获取的内容(当然这里面也可以在多一道工序就是加水印),然后再返回结果,方法结束(存入数据库)!
下面的方法就结合了远程抓取上传并增加水印的效果!
c# 代码
 
  1. /*远程保存图片*/
  2.     /// <summary>
  3.     /// 远程数据内容【包含图片】
  4.     /// </summary>
  5.     /// <param name="content"></param>
  6.     /// <returns></returns>
  7.     public string FormatContentPic(string content) 
  8.     { 
  9.         //自动保存远程图片
  10.         WebClient client = new WebClient(); 
  11.         //备用Reg:<img.*?src=([\"\'])(http:\/\/.+\.(jpg|gif|bmp|bnp))\1.*?>
  12.         Regex reg = new Regex("IMG[^>]*?src\\s*=\\s*(?:\"(?<1>[^\"]*)\"|'(?<1>[^\']*)')", RegexOptions.IgnoreCase); 
  13.         MatchCollection m = reg.Matches(content); 
  14.         foreach (Match math in m) 
  15.         { 
  16.             string imgUrl = math.Groups[1].Value; 
  17.             //在原图片名称前加YYMMDD重名名并上传
  18.             Regex regName = new Regex(@"\w+.(?:jpg|gif|bmp|png)", RegexOptions.IgnoreCase); 
  19.             string strNewImgName = DateTime.Now.ToString("yyyymmddhhmmss") + regName.Match(imgUrl).ToString(); 
  20.             if (strNewImgName.LastIndexOf(".") == -1) 
  21.             { 
  22.                 strNewImgName += ".gif"
  23.             } 
  24.             try
  25.             { 
  26.                 //保存图片
  27.                 client.DownloadFile(imgUrl,FilePath+"Image/" + strNewImgName); 
  28.                 MarkWater("Image/" + strNewImgName, "WaterPic.png"); 
  29.               //  AddWaterPic(FilePath + "Image/" + strNewImgName, FilePath + "Image/w_" + strNewImgName, FilePath + "WaterPic.png");
  30.               // AddWater(FilePath + "Image/" + strNewImgName, FilePath + "Image/x_" + strNewImgName);
  31.                 content = content.Replace(imgUrl, FilePathName + "Image/" + strNewImgName); 
  32.                 //content.Replace("\n", "").Replace("\r", "").Replace(imgUrl, FilePathName + "Image/" + strNewImgName);
  33.             } 
  34.             catch
  35.             { 
  36.             } 
  37.             finally
  38.             { 
  39.             } 
  40.             client.Dispose(); 
  41.         } 
  42.         return content; 
  43.     } 
  44. /// <summary>


文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
评论: 0 | 引用: 0 | 查看次数: 2801
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭