Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp:图像宽度属性_C#_Asp.net_Image - Fatal编程技术网

C# asp:图像宽度属性

C# asp:图像宽度属性,c#,asp.net,image,C#,Asp.net,Image,我正在尝试获取asp:图像的宽度(以像素为单位)。图像不是本地的 imgUserThumb.Width.Value 总是回到0.0,有人知道为什么吗?或者这不是正确的方法? 此外,图像正在使用“onerror”属性检查传递给它的图像URL是否有效,如果无效,则显示默认缩略图。如果未设置图像标记的宽度和高度,则将得到0 如果需要获得图像的实际大小,请将其加载到位图中,并从中获取宽度 Image img = new Bitmap("Mytest.png"); var imgwidth = img.

我正在尝试获取asp:图像的宽度(以像素为单位)。图像不是本地的

imgUserThumb.Width.Value
总是回到0.0,有人知道为什么吗?或者这不是正确的方法?
此外,图像正在使用“onerror”属性检查传递给它的图像URL是否有效,如果无效,则显示默认缩略图。

如果未设置图像标记的宽度和高度,则将得到0

如果需要获得图像的实际大小,请将其加载到位图中,并从中获取宽度

Image img = new Bitmap("Mytest.png");
var imgwidth = img.Width;
从URL编辑
我可以给它一个图片的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;
}