• 缩略图生成算法


    代码
    /// <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;
    }
  • 相关阅读:
    [BUUOJ记录] [强网杯 2019]随便注(三种方法)
    Content Security Policy (CSP)内容安全策略总结
    [HGAME Week2] Cosmos的博客后台
    [BUUOJ记录] [ACTF2020 新生赛]Include
    PHP弱类型hash比较缺陷
    CTF常见源码泄漏总结
    Sqlmap Tamper绕过脚本详解
    Golden Pyramid
    Prime Palindrome Golf
    Min and Max
  • 原文地址:https://www.cnblogs.com/ghfsusan/p/1891170.html
Copyright © 2020-2023  润新知