圖片等比例縮放


以下內容摘自博客園學之樂 轉載請注明出處:http://www.cnblogs.com/heluo/archive/2012/09/14/2684577.html

(1)從網上查找的js代碼,這段js代碼是把圖片按照規定的大小等比例不變形縮放后顯示的,原圖片大小不會變。

<img alt="" src="picture/DSCN1793.JPG"  onload="DrawImage(this);" />

這樣在html代碼中就不要指定width和height,若是指定的width和height和縮放后的大小不一樣,圖片就會自動填充滿html中指定的width和height,圖像就會變形。所以只要在js中指定了縮放后的寬度和高度,html中就不要指定了。

核心思想:oldHeight/oldWidth=newHeight/newWidth.

=》newHeight=oldHeight*newWidth/oldWidth 或者 newWidth=newHeight*oldWidth/oldHeight

 縮放的時候有兩種形式:

1、原圖片是寬400高300的,而你所要縮放的圖片是寬40.高60,也就是(oldHeight/oldWidth>=newHeight/newWidth),這個時候新圖片的寬度就按照,所要求的寬度,新圖片的高度

newWidth=newWidth;

newHeight=oldHeight*newWidth/oldWidth;

2、若oldHeight/oldWidth<newHeight/newWidth

newHeight=newHeight;

newWidth=newWidth=newHeight*oldWidth/oldHeight;

<script type="text/javascript">
        function DrawImage(ImgD) {
            //縮放成照片寬400.高300
            var nWidth = 400;
            var nHeight=300;
            var image = new Image();
            image.src = ImgD.src;
            image.width = ImgD.width;
            image.height = ImgD.height;
            if (image.width > 0 && image.height > 0) {
                //寬度與高度的比例大於或等於要求顯示的比例(說明比較寬)
                if (image.width / image.height >= nWidth / nHeight) {
                //如果寬度大於要求顯示的寬度
                    if (image.width > nWidth) {
                        ImgD.width = nWidth; //那么圖片就顯示要顯示的寬度
                        ImgD.height = (image.height * nWidth) / image.width; 
                    } else {
                        ImgD.width = image.width;
                        ImgD.height = image.height;
                    }
                }
                else {
                    //說明比較高
                    if (image.height > nHeight) {
                        ImgD.height = nHeight;
                        ImgD.width = (image.width * nHeight) / image.height;
                    } else {
                        ImgD.width = image.width;
                        ImgD.height = image.height;
                    }
                }
            }
        }
        </script>

 

(2)上傳圖片的時候,就把圖片等比例縮放為指定的大小,生成新的按比例縮放后的圖片;

//oWidth原照片的寬度 oHeight原照片的高度 newW要縮放的寬度 newH 要縮放的高度
public void bili(int oWidth, int oHeight, int newW, int newH)
        {
          
            if (oWidth > 0 && oHeight > 0)
            {
                if (oWidth / oHeight >= newW / newH)
                {
                    if (oWidth > newW)
                    {
                        _tWidth = newW;
                        _tHeight = (oHeight * newW) / oWidth;
                    }
                    else
                    {
                        _tWidth = oWidth;
                        _tHeight = oHeight;
                    }
                }
                else
                {
                    if (_tHeight > newH)
                    {
                        _tHeight = newH;
                        _tWidth = (oWidth * newH) / oHeight;
                    }
                    else
                    {
                        _tWidth = oWidth;
                        _tHeight = oHeight;
                    }
                }
            }
        }

 /// <summary>
        /// Resize圖片
        /// </summary>
        /// <param name="bmp">原始Bitmap</param>
        /// <param name="newW">新的寬度</param>
        /// <param name="newH">新的高度</param>
        /// <returns>處理以后的圖片</returns>
        public Bitmap KiResizeImage(Bitmap bmp, int newW, int newH)
        {
            try
            {
                Bitmap b = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(b);

                // 插值算法的質量
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                // 高質量
                g.SmoothingMode = SmoothingMode.HighQuality;

                g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
                g.Dispose();

                return b;
            }
            catch
            {
                return null;
            }
        }


/// <summary>
        ///  縮放
        /// </summary>
        /// <param name="imageUrl">圖片的路徑</param>
        /// <param name="savaPath">保存路徑</param>
        /// <param name="newW">新圖片的寬度</param>
        /// <param name="newH">新圖片的高度</param>
        /// <returns></returns>
        public void KiResizeImage(string imageUrl, string savePath, int newW, int newH)
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(imageUrl);

            bili(image.Width, image.Height, newW, newH);//比例縮放

            Bitmap NBM = new System.Drawing.Bitmap(image, image.Width, image.Height);
            image = KiResizeImage(NBM, _tWidth, _tHeight, Mode);
            if (File.Exists(savePath))
                File.Delete(savePath);
            image.Save(savePath, ImageFormat.Jpeg);
        }

 

 

縮放比例的計算和上面的js的一樣。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM