zoukankan      html  css  js  c++  java
  • 缩略图生成算法

    代码
    /// <summary>
    /// Creates a thumbnail from an existing image. Sets the biggest dimension of the
    /// thumbnail to either desiredWidth or Height and scales the other dimension down
    /// to preserve the aspect ratio
    /// </summary>
    /// <param name="imageStream">stream to create thumbnail for</param>
    /// <param name="desiredWidth">maximum desired width of thumbnail</param>
    /// <param name="desiredHeight">maximum desired height of thumbnail</param>
    /// <returns>Bitmap thumbnail</returns>
    public Bitmap CreateThumbnail(Bitmap originalBmp, int desiredWidth, int desiredHeight)
    {
        
    // If the image is smaller than a thumbnail just return it
        if (originalBmp.Width <= desiredWidth && originalBmp.Height <= desiredHeight)
        {
            
    return originalBmp;
        }

        
    int newWidth, newHeight;

        
    // scale down the smaller dimension
        if (desiredWidth * originalBmp.Height < desiredHeight * originalBmp.Width)
        {
            newWidth 
    = desiredWidth;
            newHeight 
    = (int)Math.Round((decimal)originalBmp.Height * desiredWidth / originalBmp.Width);
        }
        
    else
        {
            newHeight 
    = desiredHeight;
            newWidth 
    = (int)Math.Round((decimal)originalBmp.Width * desiredHeight / originalBmp.Height);
        }

        
    // This code creates cleaner (though bigger) thumbnails and properly
        
    // and handles GIF files better by generating a white background for
        
    // transparent images (as opposed to black)
        
    // This is preferred to calling Bitmap.GetThumbnailImage()
        Bitmap bmpOut = new Bitmap(newWidth, newHeight);
        
        
    using (Graphics graphics = Graphics.FromImage(bmpOut))
        {
            graphics.InterpolationMode 
    = InterpolationMode.HighQualityBicubic;
            graphics.FillRectangle(Brushes.White, 
    00, newWidth, newHeight);
            graphics.DrawImage(originalBmp, 
    00, newWidth, newHeight);
        }

        
    return bmpOut;
    }
  • 相关阅读:
    操作数组可以通过Array这个类来操作(不需要考虑数组的类型!!!)
    Servlet------>jsp自定义标签SimpleTag(jsp2.0以后的方法,1-5已经淘汰了)
    Servlet------>jsp自定义标签5(标签体内容改为大写)
    Servlet------>jsp自定义标签(JSPTAG接口)
    Servlet------>jsp自定义标签4(重复标签体)
    Servlet------>jsp自定义标签3(不显示余下jsp内容)
    模块学习
    正则表达式与re模块
    模块
    迭代器与生成器
  • 原文地址:https://www.cnblogs.com/ghfsusan/p/1891170.html
Copyright © 2011-2022 走看看