Reputation: 3896
I am trying to get the width in pixels of a an asp:Image. The image is not local.
imgUserThumb.Width.Value
Is always coming back as 0.0 anyone know why? Or perhaps thats not the right way? Also the image is using an "onerror" attribute to check if the image URL passed to it is valid, if not then it displays a default thumbnail.
Upvotes: 0
Views: 843
Reputation: 63065
If you not set width and height for the image tag, you will get 0.
if you need to get actual size of image, load it to bitmap and get the width from there.
Image img = new Bitmap("Mytest.png");
var imgwidth = img.Width;
private int GetImageWidth(string url)
{
int width = 0;
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential("name", "pw");
byte[] imageBytes = client.DownloadData(url);
using (MemoryStream stream = new MemoryStream(imageBytes))
{
var img = Image.FromStream(stream);
width= img.Width;
}
}
return width;
}
Upvotes: 1