ASP.NET:获取图像的高度和宽度

Ant*_*ony 11 asp.net image

已经问过这个问题的各种风格,但我还没有找到正确的答案.

假设我在文件服务器上有一个.jpg图像,我需要获得它的高度和宽度.我怎么在asp.net中做到这一点?

我已经看到一些答案,建议做这样的事情:

System.Drawing.Image image=System.Drawing.Image.FromFile(PicturePath); 
int ActualWidth=image.Width;
int ActualHeight=image.Height;
image.Dispose();
Run Code Online (Sandbox Code Playgroud)

这可以正常工作,除了不支持在ASP.NET服务中使用System.Drawing命名空间中的类.

那么,如何在ASP.net中获得图像的实际高度和宽度?

Hiy*_*sat 7

在aspx上添加服务器端图像控件

<asp:image ID="img1" runat="server" src="" />
Run Code Online (Sandbox Code Playgroud)

并在后面的代码给它一个src

img1.src = System.Drawing.Image.FromFile(PicturePath);

int ActualWidth = img1.Width;
int ActualHeight = img1.Height;
img1.src = "";
Run Code Online (Sandbox Code Playgroud)

  • 无论如何img1.src都不起作用.你可能意味着img1.ImageUrl (2认同)

Dan*_*ite 0

这说的是服务,而不是应用程序。这样就可以了。