等比例缩放生成缩略图

Posted 烟雨楼台^浮云往事

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了等比例缩放生成缩略图相关的知识,希望对你有一定的参考价值。

    ///
    /// 等比例缩放生成缩略图
    ///
    /// 源图路径(物理路径)
    /// 缩略图宽度
    /// 缩略图高度(依缩略图宽度等比例计算所得)
    public void MakeThumbnail(String originalImagePath, Int32 width, Int32 height)
    {
        //获取原始图片
        using (System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath))
        {
            double orwidth = (double)originalImage.Width;
            if (orwidth < 320)
            {
                width = Convert.ToInt32(orwidth);
            }
            height = Convert.ToInt32(((double)originalImage.Height) / (orwidth / (double)width));
            //缩略图画布宽高
            Int32 towidth = width;
            Int32 toheight = height;
            //原始图片写入画布坐标和宽高(用来设置裁减溢出部分)
            Int32 x = 0;
            Int32 y = 0;
            Int32 ow = originalImage.Width;
            Int32 oh = originalImage.Height;
            //原始图片画布,设置写入缩略图画布坐标和宽高(用来原始图片整体宽高缩放)
            Int32 bg_x = 0;
            Int32 bg_y = 0;
            Int32 bg_w = towidth;
            Int32 bg_h = toheight;
            //倍数变量
            double multiple = 0;
            //获取宽长的或是高长与缩略图的倍数
            if (originalImage.Width >= originalImage.Height)
            {
                multiple = (double)originalImage.Width / (double)width;
            }
            else
            {
                multiple = (double)originalImage.Height / (double)height;
            }
            //上传的图片的宽和高小等于缩略图
            if (ow <= width && oh <= height)
            {
                //缩略图按原始宽高
                bg_w = originalImage.Width;
                bg_h = originalImage.Height;
                //空白部分用背景色填充
                bg_x = Convert.ToInt32(((double)towidth - (double)ow) / 2);
                bg_y = Convert.ToInt32(((double)toheight - (double)oh) / 2);
            }
            //上传的图片的宽和高大于缩略图
            else
            {
                //宽高按比例缩放
                bg_w = Convert.ToInt32((double)originalImage.Width / multiple);
                bg_h = Convert.ToInt32((double)originalImage.Height / multiple);
                //空白部分用背景色填充
                bg_y = Convert.ToInt32(((double)height - (double)bg_h) / 2);
                bg_x = Convert.ToInt32(((double)width - (double)bg_w) / 2);
            }
            //新建一个bmp图片,并设置缩略图大小.
            using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(towidth, toheight))
            {
                //新建一个画板
                using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bitmap))
                {
                    gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;//设置高质量插值法
                    gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
                    gr.Clear(System.Drawing.ColorTranslator.Fromhtml("#F2F2F2")); //清空画布并设置背景色
                    //在指定位置并且按指定大小绘制原图片的指定部分
                    //第一个System.Drawing.Rectangle是原图片的画布坐标和宽高,第二个是原图片写在画布上的坐标和宽高,最后一个参数是指定数值单位为像素
                    gr.DrawImage(originalImage, new System.Drawing.Rectangle(bg_x, bg_y, bg_w, bg_h), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
                    Int32 index = originalImagePath.LastIndexOf(‘\\‘) + 1;
                    String path = String.Concat(originalImagePath.Substring(0, index), "App_", originalImagePath.Substring(index));
                    try
                    {
                        //获取图片类型
                        string fileExtension = System.IO.Path.GetExtension(originalImagePath).ToLower();
                        //按原图片类型保存缩略图片,不按原格式图片会出现模糊,锯齿等问题.
                        switch (fileExtension)
                        {
                            case ".gif": bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Gif); break;
                            case ".jpg": bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); break;
                            case ".bmp": bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Bmp); break;
                            case ".png": bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Png); break;
                        }
                    }
                    catch (System.Exception e)
                    {
                        throw e;
                    }
                    finally
                    {
                        originalImage.Dispose();
                        bitmap.Dispose();
                        gr.Dispose();
                    }
                }

            }
           
        }
    }

以上是关于等比例缩放生成缩略图的主要内容,如果未能解决你的问题,请参考以下文章

PHP等比例生成缩略图

php 生成缩略图

Qt之快速生成缩略图thumbnail

PHP-生成缩略图和添加水印图-学习笔记

PHP-生成缩略图和添加水印图-学习笔记

使用ThinkPHP实现生成缩略图及显示