zoukankan      html  css  js  c++  java
  • 制作缩略图、远程缩略图

    /// <summary>
    /// 制作远程缩略图
    /// </summary>
    /// <param name="url">图片URL</param>
    /// <param name="newFileName">新图路径</param>
    /// <param name="maxWidth">最大宽度</param>
    /// <param name="maxHeight">最大高度</param>
    public static void MakeRemoteThumbnailImage(string url, string newFileName, int maxWidth, int maxHeight)
    {
        Stream stream = GetRemoteImage(url);
        if(stream == null)
            return;
        Image original = Image.FromStream(stream);
        stream.Close();
        MakeThumbnailImage(original, newFileName, maxWidth, maxHeight);
    }
    /// <summary>
    /// 获取图片流
    /// </summary>
    /// <param name="url">图片URL</param>
    /// <returns></returns>
    private static Stream GetRemoteImage(string url)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.Method = "GET";
        request.ContentLength = 0;
        request.Timeout = 20000;
        HttpWebResponse response = null;
    
        try
        {
            response = (HttpWebResponse)request.GetResponse();
            return response.GetResponseStream();
        }
        catch
        {
            return null;
        }
    }
    /// <summary>
    /// 制作缩略图
    /// </summary>
    /// <param name="original">图片对象</param>
    /// <param name="newFileName">新图路径</param>
    /// <param name="maxWidth">最大宽度</param>
    /// <param name="maxHeight">最大高度</param>
    public static void MakeThumbnailImage(Image original, string newFileName, int maxWidth, int maxHeight)
    {
        Size _newSize = ResizeImage(original.Width,original.Height,maxWidth, maxHeight);
    
        using (Image displayImage = new Bitmap(original, _newSize))
        {
            try
            {
                displayImage.Save(newFileName, original.RawFormat);
            }
            finally
            {
                original.Dispose();
            }
        }
    }
    /// <summary>
    /// 计算新尺寸
    /// </summary>
    /// <param name="width">原始宽度</param>
    /// <param name="height">原始高度</param>
    /// <param name="maxWidth">最大新宽度</param>
    /// <param name="maxHeight">最大新高度</param>
    /// <returns></returns>
    private static Size ResizeImage(int width, int height, int maxWidth, int maxHeight)
    { 
        if (maxWidth <= 0)
            maxWidth = width;
        if (maxHeight <= 0)
            maxHeight = height; 
        decimal MAX_WIDTH = (decimal)maxWidth;
        decimal MAX_HEIGHT = (decimal)maxHeight;
        decimal ASPECT_RATIO = MAX_WIDTH / MAX_HEIGHT;
    
        int newWidth, newHeight;
        decimal originalWidth = (decimal)width;
        decimal originalHeight = (decimal)height;
        
        if (originalWidth > MAX_WIDTH || originalHeight > MAX_HEIGHT) 
        {
            decimal factor;
            // determine the largest factor 
            if (originalWidth / originalHeight > ASPECT_RATIO) 
            {
                factor = originalWidth / MAX_WIDTH;
                newWidth = Convert.ToInt32(originalWidth / factor);
                newHeight = Convert.ToInt32(originalHeight / factor);
            } 
            else 
            {
                factor = originalHeight / MAX_HEIGHT;
                newWidth = Convert.ToInt32(originalWidth / factor);
                newHeight = Convert.ToInt32(originalHeight / factor);
            }      
        } 
        else 
        {
            newWidth = width;
            newHeight = height;
        }
        return new Size(newWidth,newHeight);            
    }

    若对您有用,请赞助个棒棒糖~

  • 相关阅读:
    入门(一)---Java的发展史
    移除元素
    TCP的 “三次握手” 和“四次挥手”,到底是什么鬼?
    功能测试框架
    python学习笔记之--__new__方法和__init__方法
    HTTP协议状态码详解
    python学习笔记之--hasattr函数
    一文总结软件测试工程师面试前必背的面试题(持续更新中)
    MYSQL安装file /usr/share/mysql/charsets/README from install of MySQL-server-5.6.35-1.el6.x86_64 conflicts with file from package mariadb-libs-1:5.5.60-1.el7_5.x86_64报错
    centos7 安装salt起不来处理
  • 原文地址:https://www.cnblogs.com/shurun/p/11928900.html
Copyright © 2011-2022 走看看