sd_dracula
sd_dracula

Reputation: 3896

asp:Image width attribute

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

Answers (1)

Damith
Damith

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;

EDIT from URL

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

Related Questions