zoukankan      html  css  js  c++  java
  • 生成规定大小的图片(缩略图生成)

     做一购物网站,改版N次,每次改版那产品列表图的大小都会变,第一次是90*70,第二次改版又变成160*120,每次改版都得把产品图片文件夹中的2W多张图片一个一个转为对应的大小的图片,以前用的是网上找的一个方法:

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

     结果生成的列表图总不成比例,有点变形,抽出该方法单独测试,发现用哪个模式都不好,用HW肯定会变形,用W或者H的话生成的图片大小并不是需要的大小,比如一张400*200的图片,生成160*120的图片,用W模式,结果生成的图片是160*80,这个虽然看起来是不会变形的,但是在产品列表页上的img标题是固定了大小的,如:
    <img width="160" height="120"   />
    这样最后再配上那160*80的图片生成的HTML代码就为
    <img width="160" height="120" src="160_80_aaa.jpg"   />
    这样从界面上看起来又肯定会变形,因为80的高被拉到了120。用Cut模式,虽然生成的图片会是160*120,但是发现会把图像裁剪掉一部分,这个模式也不符合要求。
    于是上网找生成缩略图的方法,找啊找啊找,在JT的博客的某一篇文章里看到:

    http://www.cnblogs.com/jeffreyzhao/archive/2009/11/24/problem-of-generating-thumbnail-image.html

    测试了一下这篇文章中的最后的那个方法,结果还是不能按照我所想的生成缩略图的方法,267*248的图片生成160*120的图片,用博客里的那个方法,生成的等比例的图片大小是129*120,如果放到已经固定了大小的img标签中还是会变形,最终还是得自己来写这个生成缩略图的方法啊,嗯,应该叫生成规定大小的图像的方法吧。
    要求:
    1、不管源图像的大小,最终都要生成预先定义好的大小的图像
    2、如果源图像的宽高比预先定义的大小都要小,如 16*16 的图像生成160*120的图像,那么就相当于把16*16的图像画到160*120图像的中间,图像背景色为白色
    3、如果源图像至少有一边比预先定义好的大小要大,那么就先生成等比例缩放好的图像,然后再画到预先定义大小的图像上,如:400*200的图像生成160*120的图像,则先生成等比例的160*80的图像,然后再把该图像画到160*120图像的中间

    有了需求,再结合上面的一些两个示例代码,写出了如下方法:

    C#代码 
    1.     /// <summary>创建规定大小的图像    /// 源图像只能是JPG格式  
    2.     /// </summary>  
    3.     /// <param name="oPath">源图像绝对路径</param>  
    4.     /// <param name="tPath">生成图像绝对路径</param>  
    5.     /// <param name="width">生成图像的宽度</param>  
    6.     /// <param name="height">生成图像的高度</param>  
    7.     public void CreateImage(string oPath, string tPath, int width, int height)  
    8.     {  
    9.         Bitmap originalBmp = new Bitmap(oPath);  
    10.         // 源图像在新图像中的位置  
    11.         int left, top;  
    12.   
    13.   
    14.         if (originalBmp.Width <= width && originalBmp.Height <= height)  
    15.         {  
    16.             // 原图像的宽度和高度都小于生成的图片大小  
    17.             left = (int)Math.Round((decimal)(width - originalBmp.Width) / 2);  
    18.             top = (int)Math.Round((decimal)(height - originalBmp.Height) / 2);  
    19.   
    20.   
    21.             // 最终生成的图像  
    22.             Bitmap bmpOut = new Bitmap(width, height);  
    23.             using (Graphics graphics = Graphics.FromImage(bmpOut))  
    24.             {  
    25.                 // 设置高质量插值法  
    26.                 graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;  
    27.                 // 清空画布并以白色背景色填充  
    28.                 graphics.Clear(Color.White);  
    29.                 // 把源图画到新的画布上  
    30.                 graphics.DrawImage(originalBmp, left, top);  
    31.             }  
    32.             bmpOut.Save(tPath);  
    33.             bmpOut.Dispose();  
    34.   
    35.   
    36.             return;  
    37.         }  
    38.   
    39.   
    40.         // 新图片的宽度和高度,如400*200的图像,想要生成160*120的图且不变形,  
    41.         // 那么生成的图像应该是160*80,然后再把160*80的图像画到160*120的画布上  
    42.         int newWidth, newHeight;  
    43.         if (width * originalBmp.Height < height * originalBmp.Width)  
    44.         {  
    45.             newWidth = width;  
    46.             newHeight = (int)Math.Round((decimal)originalBmp.Height * width / originalBmp.Width);  
    47.             // 缩放成宽度跟预定义的宽度相同的,即left=0,计算top  
    48.             left = 0;  
    49.             top = (int)Math.Round((decimal)(height - newHeight) / 2);  
    50.         }  
    51.         else  
    52.         {  
    53.             newWidth = (int)Math.Round((decimal)originalBmp.Width * height / originalBmp.Height);  
    54.             newHeight = height;              
    55.             // 缩放成高度跟预定义的高度相同的,即top=0,计算left  
    56.             left = (int)Math.Round((decimal)(width - newWidth) / 2);  
    57.             top = 0;  
    58.         }  
    59.   
    60.   
    61.         // 生成按比例缩放的图,如:160*80的图  
    62.         Bitmap bmpOut2 = new Bitmap(newWidth, newHeight);  
    63.         using (Graphics graphics = Graphics.FromImage(bmpOut2))  
    64.         {  
    65.             graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;  
    66.             graphics.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight);  
    67.             graphics.DrawImage(originalBmp, 0, 0, newWidth, newHeight);  
    68.         }  
    69.         // 再把该图画到预先定义的宽高的画布上,如160*120  
    70.         Bitmap lastbmp = new Bitmap(width, height);  
    71.         using (Graphics graphics = Graphics.FromImage(lastbmp))  
    72.         {  
    73.             // 设置高质量插值法  
    74.             graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;  
    75.             // 清空画布并以白色背景色填充  
    76.             graphics.Clear(Color.White);  
    77.             // 把源图画到新的画布上  
    78.             graphics.DrawImage(bmpOut2, left, top);  
    79.         }  
    80.         lastbmp.Save(tPath);  
    81.         lastbmp.Dispose();  
    82.     }  

     测试发现好像不支持GIF格式的,我只试过JPG格式的是可以,不知道其他的如PNG,BMP之类的行不行,懒得试了,反正产品图片一般都是jpg格式的
    以上方法使用示例如下:

    C#代码 
    1.  CreateImage(Server.MapPath("~/aaa.jpg"),Server.MapPath("~/aaa_160_120.jpg"),160,120);  

     经测试,这样生成的图片就是160*120了,且不会变形。
    代码下载:http://niunan.net/download/genpic.7z

  • 相关阅读:
    解释机器学习模型的一些方法(一)——数据可视化
    机器学习模型解释工具-Lime
    Hive SQL 语法学习与实践
    LeetCode 198. 打家劫舍(House Robber)LeetCode 213. 打家劫舍 II(House Robber II)
    LeetCode 148. 排序链表(Sort List)
    LeetCode 18. 四数之和(4Sum)
    LeetCode 12. 整数转罗马数字(Integer to Roman)
    LeetCode 31. 下一个排列(Next Permutation)
    LeetCode 168. Excel表列名称(Excel Sheet Column Title)
    论FPGA建模,与面向对象编程的相似性
  • 原文地址:https://www.cnblogs.com/think_fish/p/1719485.html
Copyright © 2011-2022 走看看