• Asp.Net中图片大小的缩放


    在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;
            }
  • 相关阅读:
    zcu106 sd卡mount错误
    petalinux如何保留u-boot和kernel源码
    mali开发板
    v550 bare-metal 裸机 结构
    make打印隐含变量和隐含规则
    麒麟加速器
    petalinux定制rootfs 加入iperf memtester ethtool
    kafka分区选主机制
    java8 String intern()
    Storm之配置文件
  • 原文地址:https://www.cnblogs.com/faib/p/754191.html
Copyright © 2020-2023  润新知