1

I want to use the answer to this question here: Resize image proportionally with MaxHeight and MaxWidth constraints

But my image is a .Net image. I was wondering if there is any way to resize the image proportionally with a maxheight and maxwidth constraint for a .Net Image. Here is what the asp code looks like:

<asp:Image ID="Image1" runat="server" Height="445px" Visible="False" Width="640px" />

The code behind looks like this:

Image1.ImageUrl = "/Images/" + image1Link.ToString();
Community
  • 1
  • 1
Kevin
  • 3,209
  • 9
  • 39
  • 53

2 Answers2

1

An asp.Net Image has the same attributes of the Winforms Image. You still can use Image1.Height and Image1.Width. Just set the height & width to these attributes. check an example:

protected void Page_Load(System.Object sender, System.EventArgs e) {
    if (!Page.IsPostBack) {
        LoadImage();
    }
}

private void LoadImage(){
    Image1.ImageUrl = LoadURLFromDatabase(params);
    Image1.Width = (int)(image.Width * ratio);
    Image1.Height = (int)(image.Height * ratio);
}
Andre Figueiredo
  • 12,930
  • 8
  • 48
  • 74
  • Uhm....stupid question coming up I know. I have using System.Web.UI.WebControls.Image and public partial class ImageScaler : System.Web.UI.WebControls.Image but it says Image could not be found are you missing a reference or assembly? (I've already added the reference to System.Web to my project). – Kevin Sep 06 '13 at 02:12
  • you don't need to create a class to resize image.. just adapt the code I provided and use in ASPX Page Code Behind when you bind the source image to control. – Andre Figueiredo Sep 09 '13 at 13:49
  • 1
    Cool thanks I did that! Ok one thing, and I know this is getting nit picky and possibly very complex. You know how some websites show all pictures with the same dimensions (facebook for example, all pictures are resized to be the same and fit in the same dimensions?). How does that work? This code works great but the width/height will differ for every image based on the ratio. Yet somehow facebook pictures for example, the proportions of the image uploaded are kept and the images are all the same dimensions. – Kevin Sep 11 '13 at 03:02
  • They crop the image. You can verify that when you upload your profile photo or an album photo. In timeline, I think, they may crop the photo but when you click and the original shows up - it appears full sized. ( if you get the question resolved, mark the question as the answer ;) ) – Andre Figueiredo Sep 11 '13 at 12:11
0

If I understand your problem correctly you are trying to show image on a web page. Why not to use CSS or just set side to 100%?

<asp:Image ID="Image1" runat="server" Height="445px" Visible="False" Width="100%" />

You may experiment with "auto" setting, too.

Community
  • 1
  • 1
Na Na
  • 818
  • 3
  • 13
  • 19
  • Well I want to show all images as one size, so I set a maxHeight and maxWidth. But I want to resize the image to make it fit proportionally into that maxHeight and maxWidth – Kevin Sep 06 '13 at 02:10
  • 1
    Kevin, you want impossible. You can not show all images in one size if some of them have different aspect ratio. You can resize them but aspect ratio would not be maintained so they'd look strange. You have to decide what is more important: one size without keeping aspect ratio or one size of one side with it. – Na Na Sep 06 '13 at 07:29