生成缩略图

  1. using System;
  2. using System.Net;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.Drawing.Drawing2D;
  6. using System.Text.RegularExpressions;
  7. /// <summary>
  8. /// Image 的摘要说明
  9. /// </summary>
  10. public class MakeImage
  11. {
  12.     public MakeImage()
  13.     {
  14.         //
  15.         // TODO: 在此处添加构造函数逻辑
  16.         //
  17.     }
  18.     ///  <summary> 
  19.     /// 生成缩略图 
  20.     ///  </summary> 
  21.     ///  <param name="originalImagePath">源图路径(物理路径) </param> 
  22.     ///  <param name="thumbnailPath">缩略图路径(物理路径) </param> 
  23.     ///  <param name="width">缩略图宽度 </param> 
  24.     ///  <param name="height">缩略图高度 </param> 
  25.     ///  <param name="mode">生成缩略图的方式 </param>     
  26.     public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
  27.     {
  28.         Image originalImage = Image.FromFile(originalImagePath);
  29.         int towidth = width;
  30.         int toheight = height;
  31.         int x = 0;
  32.         int y = 0;
  33.         int ow = originalImage.Width;
  34.         int oh = originalImage.Height;
  35.         switch (mode)
  36.         {
  37.             case "HW"://指定高宽缩放(可能变形)                 
  38.                 break;
  39.             case "W"://指定宽,高按比例
  40.                 toheight = originalImage.Height * width / originalImage.Width;
  41.                 break;
  42.             case "H"://指定高,宽按比例 
  43.                 towidth = originalImage.Width * height / originalImage.Height;
  44.                 break;
  45.             case "Cut"://指定高宽裁减(不变形)                 
  46.                 if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
  47.                 {
  48.                     oh = originalImage.Height;
  49.                     ow = originalImage.Height * towidth / toheight;
  50.                     y = 0;
  51.                     x = (originalImage.Width - ow) / 2;
  52.                 }
  53.                 else
  54.                 {
  55.                     ow = originalImage.Width;
  56.                     oh = originalImage.Width * height / towidth;
  57.                     x = 0;
  58.                     y = (originalImage.Height - oh) / 2;
  59.                 }
  60.                 break;
  61.             default:
  62.                 break;
  63.         }
  64.         //新建一个bmp图片 
  65.         Image bitmap = new Bitmap(towidth, toheight);
  66.         //新建一个画板 
  67.         Graphics g = Graphics.FromImage(bitmap);
  68.         //设置高质量插值法 
  69.         g.InterpolationMode = InterpolationMode.High;
  70.         //设置高质量,低速度呈现平滑程度 
  71.         g.SmoothingMode = SmoothingMode.HighQuality;
  72.         //清空画布并以透明背景色填充 
  73.         g.Clear(Color.Transparent);
  74.         //在指定位置并且按指定大小绘制原图片的指定部分 
  75.         g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
  76.             new Rectangle(x, y, ow, oh),
  77.             GraphicsUnit.Pixel);
  78.         try
  79.         {
  80.             //以jpg格式保存缩略图 
  81.             bitmap.Save(thumbnailPath, ImageFormat.Jpeg);
  82.         }
  83.         catch (System.Exception e)
  84.         {
  85.             throw e;
  86.         }
  87.         finally
  88.         {
  89.             originalImage.Dispose();
  90.             bitmap.Dispose();
  91.             g.Dispose();
  92.         }
  93.     }
  94.     ///  <summary> 
  95.     /// 在图片上增加文字水印 
  96.     ///  </summary> 
  97.     ///  <param name="Path">原服务器图片路径 </param> 
  98.     ///  <param name="Path_sy">生成的带文字水印的图片路径 </param>
  99.     public static void AddWater(string Path, string Path_sy)
  100.     {
  101.         string addText = "文字水印";
  102.         Image image = Image.FromFile(Path);
  103.         Graphics g = Graphics.FromImage(image);
  104.         g.DrawImage(image, 0, 0, image.Width, image.Height);
  105.         Font f = new Font("Verdana", 60);
  106.         Brush b = new SolidBrush(Color.Green);
  107.         g.DrawString(addText, f, b, 35, 35);
  108.         g.Dispose();
  109.         image.Save(Path_sy);
  110.         image.Dispose();
  111.     }
  112.     ///  <summary> 
  113.     /// 在图片上生成图片水印
  114.     ///  </summary> 
  115.     ///  <param name="Path">原服务器图片路径 </param> 
  116.     ///  <param name="Path_syp">生成的带图片水印的图片路径 </param> 
  117.     ///  <param name="Path_sypf">水印图片路径 </param> 
  118.     public static void AddWaterPic(string Path, string Path_syp, string Path_sypf)
  119.     {
  120.         Image image = Image.FromFile(Path);
  121.         Image copyImage = Image.FromFile(Path_sypf);
  122.         Graphics g = Graphics.FromImage(image);
  123.         Rectangle rect = new Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height);
  124.         g.DrawImage(copyImage, rect, 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
  125.         g.Dispose();
  126.         image.Save(Path_syp);
  127.         image.Dispose();
  128.     }
  129.     /// <summary>
  130.     /// 自动保存远程图片(从html中搜索img)
  131.     /// </summary>
  132.     /// <param name="html">web page source</param>
  133.     /// <param name="directory">保存目录</param>
  134.     public static void SaveWebImage(string html, string directory)
  135.     {
  136.         WebClient client = new WebClient();
  137.         //备用Reg: <img.*?src=([/"/'])(http:.+/.(jpg |gif |bmp |bnp))/1.*?> 
  138.         Regex reg = new Regex("IMG[^>]*?src//s*=//s*(?:/"(? <1>[^/"]*)/" |'(? <1>[^/']*)')", RegexOptions.IgnoreCase);
  139.         MatchCollection m = reg.Matches(html);
  140.         string imgUrl, newImgName;
  141.         Regex regName;
  142.         string strNewImgName;
  143.         foreach (Match math in m)
  144.         {
  145.             imgUrl = math.Groups[1].Value;
  146.             //在原图片名称前加YYMMDD重名名并上传
  147.             regName = new Regex(@"/w+.(?:jpg |gif |bmp |png)", RegexOptions.IgnoreCase);
  148.             strNewImgName = DateTime.Now.ToShortDateString().Replace("-""") + regName.Match(imgUrl).ToString();
  149.             try
  150.             {
  151.                 //保存图片
  152.                 client.DownloadFile(imgUrl, directory + strNewImgName);
  153.             }
  154.             catch
  155.             {
  156.             }
  157.             client.Dispose();
  158.         }
  159.     }
  160. }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值