使用MaxHeight和MaxWidth调整图像大小

时间:2013-09-04 03:26:16

标签: c# asp.net image

我想在这里使用这个问题的答案:Resize image proportionally with MaxHeight and MaxWidth constraints

但我的图片是.Net图片。我想知道是否有任何方法可以使用.Net Image的maxheight和maxwidth约束按比例调整图像大小。这是asp代码的样子:

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

背后的代码如下所示:

Image1.ImageUrl = "/Images/" + image1Link.ToString();

2 个答案:

答案 0 :(得分:1)

asp.Net Image具有与Winforms Image相同的属性。 您仍然可以使用Image1.Height和Image1.Width。 只需设置高度&amp;这些属性的宽度。查一个例子:

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);
}

答案 1 :(得分:0)

如果我正确理解您的问题,您会尝试在网页上显示图片。为什么不使用CSS或只设置为100%?

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

您也可以尝试“自动”设置。