【问题标题】:WPF - Getting Image Width/Height from internet URLWPF - 从互联网 URL 获取图像宽度/高度
【发布时间】:2013-11-08 06:53:21
【问题描述】:

如果使用以下代码将图像存储在我的计算机中,我已经设法获取图像的宽度/高度:(完整路径是文件的完整位置)

        var bitmapFrame = BitmapFrame.Create(new Uri(FullPath), BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
        var width = bitmapFrame.PixelWidth;
        var height = bitmapFrame.PixelHeight;

但第二次我尝试将 FullPath 更改为 Internet 图像(例如 http://www.interweb.in/attachments/pc-wallpapers/16187d1222942178-nature-wallpaper-nature-summer-wallpaper.jpg)时,宽度和高度将无法确定所需的实际值,而只会保留为“1”的值。

我已经在这里坐了几个小时,试图找出问题所在并解决问题,但没有成功。 非常感谢您!

【问题讨论】:

    标签: wpf image height width


    【解决方案1】:

    试试这个。对于网络UriImageSource会异步下载图片,避免阻塞。

        var bitmapFrame = BitmapFrame.Create(new Uri(FullPath), BitmapCreateOptions.None, BitmapCacheOption.None);
        if(bitmapFrame.IsDownloading)
        {
            bitmapFrame.DownloadCompleted += (e, arg) =>{
                var width = bitmapFrame.PixelWidth;
                var height = bitmapFrame.PixelHeight;
    
            }
        }
    

    您想等待DownloadCompleted 事件。

    【讨论】:

    • 我尝试复制您的建议(我唯一更改的是将参数名称从 e 更改为 t,因为我正在单击按钮进行测试)但值仍然是 1。
    • 我发现了问题,它需要是 BitmapCreateOptions.None 而不是 BitmapCreateOptions.DelayCreation 。非常感谢:)!
    猜你喜欢
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 2014-07-15
    • 2013-06-19
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    相关资源
    最近更新 更多