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;
    }
  • 相关阅读:
    Android application context/activity context与内存泄露(转)
    Android Slide Menu相关
    android 实现自定义卫星菜单
    关于引用Theme.AppCompat theme的报错问题
    java中volatile关键字的含义
    自定义控件其实很简单1/3
    Android 修改应用程序字体
    Android字体工作原理
    颜色渐变的算法
    Android系统下载管理DownloadManager功能介绍及使用示例
  • 原文地址:https://www.cnblogs.com/ghfsusan/p/1891170.html
Copyright © 2011-2022 走看看