生成规定大小的图片(缩略图生成)

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

    /// <summary>生成缩略图
    /// 
    /// </summary>
    /// <param name="originalImagePath">源图路径(物理路径)</param>
    /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
    /// <param name="width">缩略图宽度</param>
    /// <param name="height">缩略图高度</param>
    /// <param name="mode">生成缩略图的方式</param> 
    ///         
    public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
    {
        System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

        int towidth = width;
        int toheight = height;

        int x = 0;
        int y = 0;
        int ow = originalImage.Width;
        int oh = originalImage.Height;

        switch (mode)
        {
            case "HW"://指定高宽缩放(可能变形)           
                break;
            case "W"://指定宽,高按比例             
                toheight = originalImage.Height * width / originalImage.Width;
                break;
            case "H"://指定高,宽按比例
                towidth = originalImage.Width * height / originalImage.Height;
                break;
            case "Cut"://指定高宽裁减(不变形)           
                if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                {
                    oh = originalImage.Height;
                    ow = originalImage.Height * towidth / toheight;
                    y = 0;
                    x = (originalImage.Width - ow) / 2;
                }
                else
                {
                    ow = originalImage.Width;
                    oh = originalImage.Width * height / towidth;
                    x = 0;
                    y = (originalImage.Height - oh) / 2;
                }
                break;
            default:
                break;
        }

        //新建一个bmp图片
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

        //新建一个画板
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

        //设置高质量插值法
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

        //设置高质量,低速度呈现平滑程度
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        //清空画布并以白色背景色填充
        g.Clear(System.Drawing.Color.White);

        //在指定位置并且按指定大小绘制原图片的指定部分
        g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
            new System.Drawing.Rectangle(x, y, ow, oh),
            System.Drawing.GraphicsUnit.Pixel);

        try
        {
            //以jpg格式保存缩略图
            bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (System.Exception e)
        {
            throw e;
        }
        finally
        {
            originalImage.Dispose();
            bitmap.Dispose();
            g.Dispose();
        }
    }

 结果生成的列表图总不成比例,有点变形,抽出该方法单独测试,发现用哪个模式都不好,用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图像的中间

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

    /// <summary>创建规定大小的图像    /// 源图像只能是JPG格式
    /// </summary>
    /// <param name="oPath">源图像绝对路径</param>
    /// <param name="tPath">生成图像绝对路径</param>
    /// <param name="width">生成图像的宽度</param>
    /// <param name="height">生成图像的高度</param>
    public void CreateImage(string oPath, string tPath, int width, int height)
    {
        Bitmap originalBmp = new Bitmap(oPath);
        // 源图像在新图像中的位置
        int left, top;


        if (originalBmp.Width <= width && originalBmp.Height <= height)
        {
            // 原图像的宽度和高度都小于生成的图片大小
            left = (int)Math.Round((decimal)(width - originalBmp.Width) / 2);
            top = (int)Math.Round((decimal)(height - originalBmp.Height) / 2);


            // 最终生成的图像
            Bitmap bmpOut = new Bitmap(width, height);
            using (Graphics graphics = Graphics.FromImage(bmpOut))
            {
                // 设置高质量插值法
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                // 清空画布并以白色背景色填充
                graphics.Clear(Color.White);
                // 把源图画到新的画布上
                graphics.DrawImage(originalBmp, left, top, originalBmp.Width, originalBmp.Height);
            }
            bmpOut.Save(tPath);
            bmpOut.Dispose();


            return;
        }


        // 新图片的宽度和高度,如400*200的图像,想要生成160*120的图且不变形,
        // 那么生成的图像应该是160*80,然后再把160*80的图像画到160*120的画布上
        int newWidth, newHeight;
        if (width * originalBmp.Height < height * originalBmp.Width)
        {
            newWidth = width;
            newHeight = (int)Math.Round((decimal)originalBmp.Height * width / originalBmp.Width);
            // 缩放成宽度跟预定义的宽度相同的,即left=0,计算top
            left = 0;
            top = (int)Math.Round((decimal)(height - newHeight) / 2);
        }
        else
        {
            newWidth = (int)Math.Round((decimal)originalBmp.Width * height / originalBmp.Height);
            newHeight = height;            
            // 缩放成高度跟预定义的高度相同的,即top=0,计算left
            left = (int)Math.Round((decimal)(width - newWidth) / 2);
            top = 0;
        }


        // 生成按比例缩放的图,如:160*80的图
        Bitmap bmpOut2 = new Bitmap(newWidth, newHeight);
        using (Graphics graphics = Graphics.FromImage(bmpOut2))
        {
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight);
            graphics.DrawImage(originalBmp, 0, 0, newWidth, newHeight);
        }
        // 再把该图画到预先定义的宽高的画布上,如160*120
        Bitmap lastbmp = new Bitmap(width, height);
        using (Graphics graphics = Graphics.FromImage(lastbmp))
        {
            // 设置高质量插值法
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            // 清空画布并以白色背景色填充
            graphics.Clear(Color.White);
            // 把源图画到新的画布上
            graphics.DrawImage(bmpOut2, left, top);
        }
        lastbmp.Save(tPath);
        lastbmp.Dispose();
    }

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

 CreateImage(Server.MapPath("~/aaa.jpg"),Server.MapPath("~/aaa_160_120.jpg"),160,120);

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值