【问题标题】:asp:Image width attributeasp:图像宽度属性
【发布时间】:2012-06-13 07:04:32
【问题描述】:

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

imgUserThumb.Width.Value

总是以 0.0 的形式返回,有人知道为什么吗?或者也许那不是正确的方法? 此外,图像使用“onerror”属性来检查传递给它的图像 URL 是否有效,如果不是,则显示默认缩略图。

【问题讨论】:

    标签: c# asp.net image


    【解决方案1】:

    如果你没有设置图片标签的宽高,你会得到0。

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

    Image img = new Bitmap("Mytest.png");
    var imgwidth = img.Width;
    

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

    【讨论】:

    • 我可以给它一个图片的 URL 还是必须是本地的?
    • 谢谢,我会尝试并提供反馈
    猜你喜欢
    • 2013-03-19
    • 2021-09-28
    • 2012-06-16
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多