博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Asp.Net中图片大小的缩放

Posted on 2007-05-21 14:37  faib  阅读(1974)  评论(0编辑  收藏  举报
在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;
        }