转换图片的高度和宽度 - ConvertWidthHeightClass

2017-02-26 15:38:30  访问(2373) 赞(0) 踩(0)

  • 
            /// <summary>
            /// 转换图片的高度和宽度 - ConvertWidthHeightClass
            /// </summary>
            /// <param name="cur">当前图片的高度和宽度</param>
            /// <param name="allow">页面允许的图片的最大高度和最大宽度</param>
            /// <returns></returns>
            public static WidthHeightClass ConvertWidthHeightClass
                (
                    WidthHeightClass cur, 
                    WidthHeightClass allow
                )
            {
                int returnHeight = -1;
                int returnWidth = -1;
    
                if (cur.Width <= allow.Width)
                {
                    if (cur.Height <= allow.Height)
                        return cur;
    
                    returnHeight = allow.Height;
    
                    returnWidth = (cur.Width * allow.Height) / cur.Height;
    
                    return new WidthHeightClass(returnWidth, returnHeight);
                }
                else
                {
                    if (cur.Height <= allow.Height)
                    {
                        returnWidth = allow.Width;
    
                        returnHeight = (cur.Height * allow.Width) / cur.Width;
    
                        return new WidthHeightClass(returnWidth, returnHeight);
                    }
                    else
                    {
                        returnWidth = allow.Width;
    
                        returnHeight = (cur.Height * allow.Width) / cur.Width;
    
                        if (returnHeight > allow.Height)
                        {
                            returnHeight = allow.Height;
    
                            returnWidth = (cur.Width * allow.Height) / cur.Height;
                        }
     
                        return new WidthHeightClass(returnWidth, returnHeight);
                    }
                }
            }
    

  • using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace SlowX.Functions.Classes
    {
        /// <summary>
        /// 宽和高的结构 - WidthHeightClass + 
        /// </summary>
        [Serializable]
        public class WidthHeightClass
        {
            /// <summary>
            /// 宽和高的结构
            /// </summary>
            public WidthHeightClass()
            {
    
            }
    
            /// <summary>
            /// 宽和高的结构
            /// </summary>
            /// <param name="_Width"></param>
            /// <param name="_Height"></param>
            public WidthHeightClass(int _Width, int _Height)
            {
                m_Width = _Width;
    
                m_Height = _Height;
            }
    
            /// <summary>
            /// 宽
            /// </summary>
            protected int m_Width = 0;
    
            /// <summary>
            /// 宽
            /// </summary>
            public int Width
            {
                get
                {
                    return m_Width;
                }
                set
                {
                    m_Width = value;
                }
            }
    
            /// <summary>
            /// 高
            /// </summary>
            protected int m_Height = 0;
    
            /// <summary>
            /// 高
            /// </summary>
            public int Height
            {
                get
                {
                    return m_Height;
                }
                set
                {
                    m_Height = value;
                }
            }
        }
    }
    
    

标签:转换图片的高度和宽度 - ConvertWidthHeightClass 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)