Scaling of images in ASP. NET
Last Update:2018-12-07
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;
}