zoukankan      html  css  js  c++  java
  • C#创建高质量(清晰)缩略图——动态调整大小

    创建高质量(清晰)缩略图——动态调整大小
    Bitmap类的GetThumbnailImage方法可以从指定的图像文件中生成缩略图,用法非常简单。但是并不总是这样,有时生成的缩略图的质量很差——模糊不清!
    为什么会这样?为什么生成的图片是模糊的?
    像JPEG这样格式的图像可能把缩略图存在同一个文件中。如果我们使用System.Drawing.Bitmap的GetThumbnailImage方法会检测文件中是否存在缩略图,如果找到该缩略图,它就会返回你所设定的宽高的缩略图版本。如果图像的缩略图的版本比你要求的大小要小,就会发生如下问题:生成的缩略图就会变得模糊,我们知道拉伸一个图像会导致图像质量的下降。
    解决方案:
    解决方法十分简单!我们利用System.Drawing.Image对象来装载源图像, 把图像缩放到你需要的比例,而又能保持高质量,接着保存就搞定了!
    下面教你,一步一步来创建高质量的缩略图:
    本例中假定了两个变量:
    String src='c:/myImages/a.jpg'; //源图像文件的绝对路径
    String dest='c:/myImages/a_th.jpg'; //生成的缩略图图像文件的绝对路径
    int thumbWidth=132; //要生成的缩略图的宽度
    下面通过ASP .NET C#来演示代码片段:
    你应该包含这些名称空间
    using System.Drawing;
    using System.Drawing.Drawing2D
    System.Drawing.Image image = System.Drawing.Image.FromFile(src); //利用Image对象装载源图像
    //接着创建一个System.Drawing.Bitmap对象,并设置你希望的缩略图的宽度和高度。
    int srcWidth=image.Width;
    int srcHeight=image.Height;
    int thumbHeight=(srcHeight/srcWidth)*thumbWidth;
    Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);
    //从Bitmap创建一个System.Drawing.Graphics对象,用来绘制高质量的缩小图。
    System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
    //设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality
    gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    //下面这个也设成高质量
    gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
    //下面这个设成High
    gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    //把原始图像绘制成上面所设置宽高的缩小图
    System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
    gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
    //保存图像,大功告成!
    bmp.Save(dest);
    //最后别忘了释放资源(译者PS:C#可以自动回收吧)
    bmp.Dispose();
    image.Dispose();

    来源:http://blog.sina.com.cn/s/blog_6154bf970100ot9r.html

     /// <summary>
            /// 生成高清缩略图
            /// </summary>
            /// <param name="originalImagePath">源图路径(物理路径)</param>
            /// <param name="thumbnailPath">保存路径(物理路径)</param>
            /// <param name="width">缩略图宽度</param>
            /// <param name="height">缩略图高度</param>
            public static void MakeHighDefinitionThumbnail(string originalImagePath, string thumbnailPath, int width, int height)
            {
                //获取原始图片 
                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;
                //原始图片画布,设置写入缩略图画布坐标和宽高(用来原始图片整体宽高缩放) 
                int bg_x = 0;
                int bg_y = 0;
                int bg_w = towidth;
                int 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图片,并设置缩略图大小. 
                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.HighQualityBilinear;
                //设置高质量,低速度呈现平滑程度 
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //清空画布并设置背景色 
                g.Clear(System.Drawing.ColorTranslator.FromHtml("#F2F2F2"));
                //在指定位置并且按指定大小绘制原图片的指定部分 
                //第一个System.Drawing.Rectangle是原图片的画布坐标和宽高,第二个是原图片写在画布上的坐标和宽高,最后一个参数是指定数值单位为像素 
                g.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);
                try
                {
                    //获取图片类型 
                    string fileExtension = System.IO.Path.GetExtension(originalImagePath).ToLower();
                    //按原图片类型保存缩略图片,不按原格式图片会出现模糊,锯齿等问题. 
                    switch (fileExtension)
                    {
                        case ".gif": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif); break;
                        case ".jpg": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); break;
                        case ".bmp": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp); break;
                        case ".png": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png); break;
                    }
                }
                catch (System.Exception e)
                {
                    throw e;
                }
                finally
                {
                    originalImage.Dispose();
                    bitmap.Dispose();
                    g.Dispose();
                }
            }

  • 相关阅读:
    如何讓你的程序在退出的時候執行一段代碼?
    05_Python爬蟲入門遇到的坑__總結
    04_Python爬蟲入門遇到的坑__向搜索引擎提交關鍵字02
    03_Python爬蟲入門遇到的坑__向搜索引擎提交關鍵字01
    02_Python爬蟲入門遇到的坑__反爬蟲策略02
    01_Python爬蟲入門遇到的坑__反爬蟲策略01
    Python爬蟲--rrequests庫的基本使用方法
    C#筆記00--最基礎的知識
    為元組中的每一個元素命名
    Filter函數
  • 原文地址:https://www.cnblogs.com/lidj/p/3441409.html
Copyright © 2011-2022 走看看