• 批量生成缩略图


    .net实现批量生成多种格式缩略图,自动根据宽高做判断,固定宽或高,高或宽按比例生成

       1.实现代码:

     1.1.调用方法:

    http://localhost/Default.aspx?year=2010&month=8&day=8

       1.2.web.config配置

    <httpRuntime executionTimeout="9000" maxRequestLength="2000000" useFullyQualifiedRedirectUrl="false" requestLengthDiskThreshold="819200"/>

       1.3.default.aspx.cs

    001 using System;
    002 using System.Collections;
    003 using System.Configuration;
    004 using System.Data;
    005   
    006 using System.Web;
    007 using System.Web.Security;
    008 using System.Web.UI;
    009 using System.Web.UI.HtmlControls;
    010 using System.Web.UI.WebControls;
    011 using System.Web.UI.WebControls.WebParts;
    012   
    013 using System.IO;
    014 using System.Net;
    015 using System.Text.RegularExpressions;
    016   
    017 public partial class _Default : System.Web.UI.Page
    018 {
    019   
    020     protected void Page_Load(object sender, EventArgs e)
    021     {
    022         if (!Page.IsPostBack)
    023         {
    024             //调用方法:http://localhost/Default.aspx?year=2010&month=8&day=8
    025   
    026             string year = HttpContext.Current.Request.QueryString["year"];
    027             string month = HttpContext.Current.Request.QueryString["month"];
    028             string day = HttpContext.Current.Request.QueryString["day"];
    029   
    030             OpratePic(year, month, day);
    031   
    032         }
    033     }
    034   
    035     protected void OpratePic(string year, string month, string day)
    036     {
    037         string treePath = HttpContext.Current.Server.MapPath("") + "\\pic\\";
    038         string otherPath = HttpContext.Current.Server.MapPath("")+ "\\slt\\";
    039   
    040         if (!string.IsNullOrEmpty(year) && !string.IsNullOrEmpty(month) && !string.IsNullOrEmpty(day))
    041         {
    042             string treePath1 = treePath + year.ToString() + "\\" + month.ToString() + "\\" + day.ToString() + "\\";
    043             string otherPath1 = otherPath + year.ToString() + "\\" + month.ToString() + "\\" + day.ToString() + "\\";
    044   
    045             GeneratePic(treePath1, otherPath1);
    046   
    047             Response.Write("生成成功<br/>");
    048         }
    049         else if (!string.IsNullOrEmpty(year) && !string.IsNullOrEmpty(month))
    050         {
    051             treePath = treePath + year.ToString() + "\\" + month.ToString() + "\\";
    052             otherPath = otherPath + year.ToString() + "\\" + month.ToString() + "\\";
    053   
    054             for (int c = 1; c < 32; c++)
    055             {
    056                 string treePath1 = treePath + c.ToString() + "\\";
    057                 string otherPath1 = otherPath + c.ToString() + "\\";
    058   
    059                 GeneratePic(treePath1, otherPath1);
    060             }
    061   
    062             Response.Write("生成成功<br/>");
    063         }
    064         else if (!string.IsNullOrEmpty(year))
    065         {
    066             treePath = treePath + year.ToString() + "\\";
    067             otherPath = otherPath + year.ToString() + "\\";
    068   
    069             for (int b = 1; b < 13; b++)
    070             {
    071   
    072                 for (int c = 1; c < 32; c++)
    073                 {
    074                     string treePath1 = treePath +  "\\" + b.ToString() + "\\" + c.ToString() + "\\";
    075                     string otherPath1 = otherPath  + "\\" + b.ToString() + "\\" + c.ToString() + "\\";
    076   
    077                     GeneratePic(treePath1, otherPath1);
    078                 }
    079             }
    080   
    081             Response.Write("生成成功<br/>");
    082         }
    083         else
    084         {
    085             Response.Write("输入参数有误!<br/>");
    086             Response.Write("缩略图格式如下:<br/>");
    087             Response.Write("http://photo.jmw.com.cn/default.aspx?year=2008&month=12&day=20<br/>");
    088         }
    089   
    090     }
    091   
    092     protected void GeneratePic(string treePath, string otherPath)
    093     {
    094   
    095         if (Directory.Exists(treePath))
    096         {
    097   
    098   
    099             string[] stringFiles = Directory.GetFiles(treePath);
    100   
    101             foreach (string stringFile in stringFiles)
    102             {
    103                 FileInfo file = new FileInfo(stringFile);
    104   
    105   
    106                 if (file.Length > 0)
    107                 {
    108                     //(||)jpgbmpgif
    109                     if (Regex.IsMatch(file.Name, @".*\.(?:jpg|gif|jpeg|bmp|png)$", RegexOptions.IgnoreCase))
    110                     {
    111   
    112                         string webFilePath = treePath + file.Name;          // 服务器端文件路径
    113                         string webFilePath_s1 = otherPath + "S1_" + file.Name;    // 服务器端缩略图路径
    114                         string webFilePath_s2 = otherPath + "S2_" + file.Name;
    115                         string webFilePath_s3 = otherPath + "S3_" + file.Name;
    116   
    117                         string webFilePath_s11 = otherPath + "S1_1_" + file.Name;
    118                         string webFilePath_s22 = otherPath + "S2_2_" + file.Name;
    119                         string webFilePath_s33 = otherPath + "S3_3_" + file.Name;
    120   
    121                         if (!Directory.Exists(otherPath)) //如果目录不存在则创建
    122                         {
    123                             Directory.CreateDirectory(otherPath);
    124                         }
    125   
    126   
    127                         MakeThumbnail(webFilePath, webFilePath_s22, 250, 188, "W");     // 生成缩略图方法W:指定宽,高按比例
    128                         MakeThumbnail(webFilePath_s22, webFilePath_s2, 250, 188, "Cut");
    129   
    130                         MakeThumbnail(webFilePath, webFilePath_s11, 152, 114, "W");     // 生成缩略图方法W:指定宽,高按比例
    131                         MakeThumbnail(webFilePath_s11, webFilePath_s1, 152, 114, "Cut");
    132   
    133                         MakeThumbnail(webFilePath, webFilePath_s33, 38, 29, "W");     // 生成缩略图方法W:指定宽,高按比例
    134                         MakeThumbnail(webFilePath_s33, webFilePath_s3, 38, 29, "Cut");
    135   
    136    
    137   
    138   
    139                     }
    140                 }
    141             }
    142   
    143   
    144         }
    145     }
    146   
    147     #region 生成缩略图
    148   
    149     /// 〈summary>
    150     /// 生成缩略图
    151     /// 〈/summary>
    152     /// 〈param name="originalImagePath">源图路径(物理路径)〈/param>
    153     /// 〈param name="thumbnailPath">缩略图路径(物理路径)〈/param>
    154     /// 〈param name="width">缩略图宽度〈/param>
    155     /// 〈param name="height">缩略图高度〈/param>
    156     /// 〈param name="mode">生成缩略图的方式〈/param>    
    157     public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
    158     {
    159   
    160         try
    161         {
    162   
    163             using (System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath))
    164             {
    165   
    166                 if (mode != "Cut")
    167                 {
    168                     double d1 = double.Parse(width.ToString()) / double.Parse(height.ToString());
    169   
    170   
    171                     //宽高比
    172                     double wd = double.Parse(originalImage.Width.ToString()) / double.Parse(originalImage.Height.ToString());
    173                     //高宽比
    174                     double hd = double.Parse(originalImage.Height.ToString()) / double.Parse(originalImage.Width.ToString());
    175   
    176   
    177                     double itsw = 0d;
    178                     double itsh = 0d;
    179                     int itsin = 0;
    180   
    181                     //与宽高比对比
    182                     if (d1 > wd)
    183                     {
    184                         itsw = d1 - wd;
    185                     }
    186                     else
    187                     {
    188                         itsw = wd - d1;
    189                     }
    190   
    191                     //与高宽比对比
    192                     if (d1 > hd)
    193                     {
    194                         itsh = d1 - hd;
    195                     }
    196                     else
    197                     {
    198                         itsh = hd - d1;
    199                     }
    200   
    201                     //如果高宽比更接近比例
    202                     if (itsw > itsh)
    203                     {
    204                         mode = "W";
    205                     }
    206                     else
    207                     {
    208                         mode = "H";
    209                     }
    210                 }
    211   
    212   
    213                 int towidth = width;
    214                 int toheight = height;
    215   
    216                 int x = 0;
    217                 int y = 0;
    218                 int ow = originalImage.Width;
    219                 int oh = originalImage.Height;
    220   
    221                 switch (mode)
    222                 {
    223                     case "HW"://指定高宽缩放(可能变形)                
    224                         break;
    225                     case "W"://指定宽,高按比例                    
    226                         toheight = originalImage.Height * width / originalImage.Width;
    227                         break;
    228                     case "H"://指定高,宽按比例
    229                         towidth = originalImage.Width * height / originalImage.Height;
    230                         break;
    231                     case "Cut"://指定高宽裁减(不变形)                
    232                         if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
    233                         {
    234                             oh = originalImage.Height;
    235                             ow = originalImage.Height * towidth / toheight;
    236                             y = 0;
    237                             x = (originalImage.Width - ow) / 2;
    238                         }
    239                         else
    240                         {
    241                             ow = originalImage.Width;
    242                             oh = originalImage.Width * height / towidth;
    243                             x = 0;
    244                             y = (originalImage.Height - oh) / 2;
    245                         }
    246                         break;
    247                     default:
    248                         break;
    249                 }
    250   
    251                 //新建一个bmp图片
    252                 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
    253   
    254                 //新建一个画板
    255                 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
    256   
    257                 //设置高质量插值法
    258                 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    259   
    260                 //设置高质量,低速度呈现平滑程度
    261                 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    262   
    263                 //清空画布并以透明背景色填充
    264                 g.Clear(System.Drawing.Color.Transparent);
    265   
    266                 //在指定位置并且按指定大小绘制原图片的指定部分
    267                 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
    268                     new System.Drawing.Rectangle(x, y, ow, oh),
    269                     System.Drawing.GraphicsUnit.Pixel);
    270   
    271                 try
    272                 {
    273                     //以jpg格式保存缩略图
    274                     bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
    275                 }
    276                 catch (System.Exception e)
    277                 {
    278   
    279                     HttpContext.Current.Response.Write(e.ToString() + thumbnailPath);
    280                 }
    281                 finally
    282                 {
    283                     originalImage.Dispose();
    284                     bitmap.Dispose();
    285                     g.Dispose();
    286                 }
    287             }
    288         }
    289         catch
    290         {
    291   
    292         }
    293     }
    294   
    295     #endregion
    296   
    297 }

     2.相关方法

    01 //根据url获取文件名
    02 public static string GetUrlFileName(string url)
    03     {
    04         if (string.IsNullOrEmpty(url))
    05         {
    06             return "";
    07         }
    08         string[] strs1 = url.Split(new char[] { '/' });
    09         return strs1[strs1.Length - 1].Split(new char[] { '?' })[0];
    10   
    11     }
  • 相关阅读:
    android 中webview调用js
    android apk打包之后js调用失效的解决办法
    android软键盘的管理和属性的设置
    android Bitmap(将视图转为bitmap对象)
    android的四种加载模式
    android 从其他app接收分享的内容
    android ADT 设置编辑字体
    android的JNI标准 android的NDK
    iOS开发随笔--iOS捕获异常、常用的异常处理方法
    IOS学习笔记--Objective-C之协议、代码块、分类
  • 原文地址:https://www.cnblogs.com/scgw/p/1915456.html
Copyright © 2020-2023  润新知