Scaling of images in ASP. NET

Source: Internet
Author: User
When an image is displayed in ASP. NET, if a fixed-size container such as <Table> is specified, how can the image size be scaled proportionally according to the container size. The following is a simple function. It is calculated based on the image width and height ratio, and the ratio remains unchanged after scaling. // Viewsize outer frame size
// Imagesize: the actual size of the image.
Public Size resize (size viewsize, size imagesize)
{
Size mysize =   New Size ();
If (Viewsize. Width > Viewsize. Height) // Larger than high
{
If (Imagesize. Width > Imagesize. Height) // Proportional
{
Float Scale = Imagesize. Height / ( Float ) Imagesize. width;
If (Viewsize. Height / ( Float ) Viewsize. Width < Scale)
{< br> mysize. height = viewsize. height;
mysize. width = ( int ) (viewsize. height / scale );
}
Else
{< br> mysize. width = viewsize. width;
mysize. height = ( int ) (viewsize. width * scale );
}
}
Else   If (Imagesize. Height > Imagesize. width) // Non-proportional
{
Float Scale = Imagesize. Width / ( Float ) Imagesize. height;
Mysize. Height = Viewsize. height;
Mysize. Width = ( Int ) (Viewsize. Height * Scale );
}
Else // Fang
{
Mysize. Height=Viewsize. height;
Mysize. Width=Viewsize. height;
}
}
Else   If (Viewsize. Width < Viewsize. Height) // Tall and wide
{
If (Imagesize. Width < Imagesize. Height) // Proportional
{
Float Scale = Imagesize. Width / ( Float ) Imagesize. height;
If (Viewsize. Width / ( Float ) Viewsize. Height < Scale)
{< br> mysize. width = viewsize. width;
mysize. height = ( int ) (viewsize. width / scale );
}
Else
{< br> mysize. height = viewsize. height;
mysize. width = ( int ) (viewsize. height * scale );
}
}
Else   If (Imagesize. Height < Imagesize. width) // Non-proportional
{
Float Scale = Imagesize. Height / ( Float ) Imagesize. width;
Mysize. Width = Viewsize. width;
Mysize. Height = ( Int ) (Viewsize. Width * Scale );
}
Else // Fang
{
Mysize. Height=Viewsize. width;
Mysize. Width=Viewsize. width;
}
}
Else // Fang
{
If (Imagesize. Width > Imagesize. Height) // Larger than high
{
Float Scale = Imagesize. Height / ( Float ) Imagesize. width;
Mysize. Width = Viewsize. width;
Mysize. Height = ( Int ) (Viewsize. Width * Scale );
}
Else   If (Imagesize. Width < Imagesize. Height) // Tall and wide
{
Float Scale = Imagesize. Width / ( Float ) Imagesize. height;
Mysize. Height = Viewsize. height;
Mysize. Width = ( Int ) (Viewsize. Height * Scale );
}
Else // Fang
{
Mysize. Height=Viewsize. height;
Mysize. Width=Viewsize. height;
}
}
Return mysize;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.