Search code examples
c#asp.netimaging

Resize <asp:Image> based on users screen resolution


I would like to show an image on a screen, and it's size (Height and Width) must be adjusted to fit on the screen (Stretch or shrink). The image has been saved to the server, and is 1024 x 768 (example).

So, at the moment, I do this:

ImageUtilities.Dimension d = 
    ImageUtilities.ImageUtilities.GetImageSize(f.FullName);
var newD = ImageUtilities.ImageUtilities.GetResized(d.Height, d.Width, 520, 520);
Image1.Height = newD.Height;
Image1.Width = newD.Width;

So, at the moment, I am forcing my image to fit in a square sized 800 x 800 (I take into account Portrait vs Landscape, and use ratio to maintain aspect.

Problem is, on a low res screen, that, the user has to scroll a bit to get to the bottom the image (where I have a Close button). And on a very high res scree, I could have maintained 1024 by 1024 as usable area.

Is there a way to get the screen res, and take those parameters into my code behind method (GetResized is a method that returns me a new Height and Width)?

I understand the user may not have his browser maximused - that's OK.


Solution

  • You can define the Image size in percentage which frees you from this stuff.