Asp.net 圖片等比例產生縮圖,外帶一個數學公式

來源:互聯網
上載者:User

   今天在做一個網站後台,有個將上傳圖片產生縮圖的功能,產生方法是以前自己寫的,結果發現不少圖片沒能正確產生,於是就調試其兩年多前寫的代碼,雖然當年寫了不少注釋,不過還是花了我不少時間,因為期間我花了不少時間來證明一個數學公式了,這個公式應該是初中的知識!想想寫了三,四的年的程式,基本上除了小學的算術,貌似就沒用到什麼資料知識了.

 這是一個不等式 
 設 A=(y*a/x)-b , B=(x * b/y)-a ,其中y,x,a,b 多為大於零的實數,
 證明在符合取值的情況下,A跟B中必定存在 A>=0 並且 B<=0 ,或者A<=0 並且 B>=0

 證明相信不難,因為這的確是一個初中時的不等公式,下面是問題的來源
 
 圖片等比例生產縮圖的代碼
//------------------------------

        public static bool ThumImage(string srcPath, string destPath, double width, double height)
        {
            System.Drawing.Image img = new Bitmap(srcPath);
            //產生圖片大小必需小於原圖
            if (img.Width < width)
            {
                width = img.Width;

            }
            if (img.Height < height)
            {
                height = img.Height;
            }
            //刪除的高度,與寬度
            double cutWidth, cutHeight;
            cutWidth = (img.Width * height / img.Height - width); //寬度切割,高度縮放
            cutHeight = (img.Height * width / img.Width - height);//高度切割,寬度縮放
            byte flag = 0;//0 截高,1 截寬
            //這裡的截寬是指縮圖的寬將被截,不是指原圖,
            //1. 按原圖比例,選擇縮圖的高固定,計算出來的寬如果大於指定的寬,那麼就按高固定,計算出來的寬來產生縮圖,再按給定大小截取
            //2. 按原圖比例,選擇縮圖的寬固定,計算出來的高如果大於指定的高,那麼就按寬固定,計算出來的高來產生縮圖,再按給定大小截取
            //3. 因為長寬比只能取{1,>1,<1}三種情況
            flag = (byte)(cutHeight <= cutWidth ? 0 : 1);

            //System.Drawing.Image.GetThumbnailImageAbort myCallback=new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
            System.Drawing.Image thumImg;
            if (flag == 0)
                thumImg = new Bitmap(img, (int)width, (int)height + (int)cutHeight); //img.GetThumbnailImage((int)width, (int)height + (int)cutHeight, myCallback, IntPtr.Zero);
            else
                thumImg = new Bitmap(img, (int)width + (int)cutWidth, (int)height);// img.GetThumbnailImage((int)width + (int)cutWidth, (int)height, myCallback, IntPtr.Zero);
            System.Drawing.Bitmap destImg = new Bitmap((int)width, (int)height);
            Graphics g = Graphics.FromImage(destImg);
            Rectangle rect = new Rectangle(0, 0, (int)width, (int)height);
            g.DrawImage(thumImg, rect, rect, GraphicsUnit.Pixel);
            g.Save();
            destImg.Save(destPath, GetFormat(destPath));
            thumImg.Dispose();
            img.Dispose();
            destImg.Dispose();
            return true;

        }

//----------------------------
 這個方法的思路是,原圖跟需要產生縮圖的長寬比例不一定相同,那麼在長寬比例不同的情況下,直接產生會使圖片變形,
為了防止這個情況發生,就需要根據原圖比例來產生,我們先根據原圖比例,產生一個略微比需要縮圖大的圖片,接著將這個圖片按指定的大小進行截取. 為實現這個需要,先根據指定縮圖的高計算出符合原圖比例的寬,將這個寬跟需要產生縮圖的寬進行比較,如果計算出來的寬大於指定縮圖的寬,那麼我們就按照 ,計算所得的寬跟縮圖的高建立一個位元影像,再根據需要的縮圖大小截取.如果計算出來的寬小於指定縮圖的寬,那麼就使用指定縮圖的寬來計算符合比例的高,此時高必定大於需要產生縮圖的高,此時按計算所得的高跟需要產生縮圖的寬,進行上面的操作,因為高跟寬必定有一個符合上面的要求. 代碼如下
        if (flag == 0)
                thumImg = new Bitmap(img, (int)width, (int)height + (int)cutHeight); //img.GetThumbnailImage((int)width, (int)height + (int)cutHeight, myCallback, IntPtr.Zero);
            else
                thumImg = new Bitmap(img, (int)width + (int)cutWidth, (int)height);// img.GetThumbnailImage((int)width + (int)cutWidth, (int)height, myCallback, IntPtr.Zero);
            System.Drawing.Bitmap destImg = new Bitmap((int)width, (int)height);
            Graphics g = Graphics.FromImage(destImg);
            Rectangle rect = new Rectangle(0, 0, (int)width, (int)height);
            g.DrawImage(thumImg, rect, rect, GraphicsUnit.Pixel);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.