close

用此function 傳回的size 型別 要引用

using System.Drawing.Imaging;

用傳回的尺寸去做縮圖的寬高就會是等比縮小的囉

 

 


        /// <summary>
        /// 等比例縮圖
        /// </summary>
        /// <param name="width">圖原寬</param>
        /// <param name="height">圖原高</param>
        /// <param name="maxWidth">要縮的寬</param>
        /// <param name="maxHeight">要縮的高</param>
        /// <returns></returns>
        private Size ResizeImage(int width, int height, int maxWidth, int maxHeight)  
        {

         decimal MAX_WIDTH = maxWidth;  

         decimal MAX_HEIGHT = maxHeight;  

         decimal ASPECT_RATIO = MAX_WIDTH / MAX_HEIGHT;  

    

         int newWidth, newHeight;  

    

         decimal originalWidth = width;  

         decimal originalHeight = 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);  

    

     }

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 11 的頭像
    11

    冠霖的部落格

    11 發表在 痞客邦 留言(0) 人氣()