Asp.Net中圖片大小的縮放

來源:互聯網
上載者:User
在Asp.Net中顯示圖片的時候,如果給定一個固定大小的容器,如<Table>,圖片的大小如何根據容器的大小進行比例縮放呢。以下是一個比較簡單的函數,根據圖片的寬高比例進行計算,縮放後保持比例不變。        //ViewSize 外框大小
        //ImageSize 圖片的實際大小
        public Size Resize(Size ViewSize, Size ImageSize)
        {
            Size MySize = new Size();
            if(ViewSize.Width > ViewSize.Height)//寬大於高
            {
                if(ImageSize.Width > ImageSize.Height)//按比例
                {
                    float scale = ImageSize.Height / (float)ImageSize.Width;
                    if(ViewSize.Height / (float)ViewSize.Width < scale)
                    {
                        MySize.Height = ViewSize.Height;
                        MySize.Width = (int)(ViewSize.Height / scale);
                    }
                    else
                    {
                        MySize.Width = ViewSize.Width;
                        MySize.Height = (int)(ViewSize.Width * scale);
                    }
                }
                else if(ImageSize.Height > ImageSize.Width)//非比例
                {
                    float scale = ImageSize.Width / (float)ImageSize.Height;
                    MySize.Height = ViewSize.Height;
                    MySize.Width = (int)(ViewSize.Height * scale);
                }
                else//正方
                {
                    MySize.Height = ViewSize.Height;
                    MySize.Width = ViewSize.Height;
                }
            }
            else if(ViewSize.Width < ViewSize.Height)//高大於寬
            {
                if(ImageSize.Width < ImageSize.Height)//按比例
                {
                    float scale = ImageSize.Width / (float)ImageSize.Height;
                    if(ViewSize.Width / (float)ViewSize.Height < scale)
                    {
                        MySize.Width = ViewSize.Width;
                        MySize.Height = (int)(ViewSize.Width / scale);
                    }
                    else
                    {
                        MySize.Height = ViewSize.Height;
                        MySize.Width = (int)(ViewSize.Height * scale);
                    }
                }
                else if(ImageSize.Height < ImageSize.Width)//非比例
                {
                    float scale = ImageSize.Height / (float)ImageSize.Width;
                    MySize.Width = ViewSize.Width;
                    MySize.Height = (int)(ViewSize.Width * scale);
                }
                else//正方
                {
                    MySize.Height = ViewSize.Width;
                    MySize.Width = ViewSize.Width;
                }
            }
            else//正方
            {
                if(ImageSize.Width > ImageSize.Height)//寬大於高
                {
                    float scale = ImageSize.Height / (float)ImageSize.Width;
                    MySize.Width = ViewSize.Width;
                    MySize.Height = (int)(ViewSize.Width * scale);
                }
                else if(ImageSize.Width < ImageSize.Height)//高大於寬
                {
                    float scale = ImageSize.Width / (float)ImageSize.Height;
                    MySize.Height = ViewSize.Height;
                    MySize.Width = (int)(ViewSize.Height * scale);
                }
                else//正方
                {
                    MySize.Height = ViewSize.Height;
                    MySize.Width = ViewSize.Height;
                }
            }
            return MySize;
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.