【问题标题】:Image not loading in Windows Phone - AG_E_NETWORK_ERROR图像未在 Windows Phone 中加载 - AG_E_NETWORK_ERROR
【发布时间】:2013-07-28 14:47:48
【问题描述】:

我已经为这个案例创建了一个测试项目。我的 .xaml 中有一个像这样的图像控件:

<Image x:Name="img" />

我已经用 6 张图片测试了这个项目,所有这些图片都来自同一个网站。显示的图像大小约为 50 - 90 KB。未显示的图像为 294 KB。

我正在这样设置图像的来源:

img.Source = new BitmapImage(new Uri(imageURI));

可能是什么问题? 谢谢。

更新1:

另外,我已经检查了 ImageFailed 事件。它正在抛出 AG_E_NETWORK_ERROR 异常。

更新 2:

这是未显示的图像来源: (已删除)

【问题讨论】:

  • 为什么不尝试异步加载图片呢?您必须安装在 Nuget 中可用的 BCL Async 库。有一个指南here
  • 请注意,渲染限制为 2000x2000 像素。所以问题可能不是你的文件大小!
  • @NateDiamond 感谢您的指导。但是,本指南中没有使用 BCL 异步库。
  • @ClausJørgensen 那么,可能是什么问题?如果你愿意,我可以提供图片来源。
  • 我认为您确实需要提供一个示例图像源,以便我们在这里提供任何帮助。

标签: c# silverlight windows-phone-7


【解决方案1】:

image in question 开启了热链接保护。

这很可能是阻止您下载它的罪魁祸首。考虑到热链接保护,我猜你也没有在应用程序中使用它的必要权限。

如果您希望解决此问题,请使用 HttpWebRequest 类并设置 HttpWebRequest.Referer 属性。

【讨论】:

  • 感谢您的回答。但是你怎么知道图片有保护?
  • 点击该链接会得到一个 HTTP 403 - Forbidden 错误代码。您看不到它的原因是,如果您将链接粘贴到浏览器中,则会跳过热链接保护。
  • 那么,我可以使用这些类绕过保护吗?
  • 是的,您应该可以这样做。但是,我想再次强调,未经书面许可在应用程序中使用第 3 方内容是不合法的。
  • 好的,谢谢。我正在尝试为他们开发一个简单的应用程序,因为他们没有任何适用于 Windows Phone 的应用程序。我会向他们解释情况。如果他们不给予许可,我不会使用它。非常感谢您的帮助。
【解决方案2】:

感谢@Claus Jørgensen,我了解到一些网站可以使用热链接保护来防止其他网站直接链接到您网站上的文件和图片。 所以我创建了一个 AttachedProperty 用于将 Image 的源绑定到 URI 并异步下载。

这里是 .xaml:

<Image AttachedProperties:ImageProperties.SourceWithCustomReferer="{Binding Image, Mode=TwoWay}"/>

和附加属性:

public static class ImageProperties
{
    #region SourceWithCustomReferer Property
    public static Dictionary<Uri, BitmapImage> imageCache = new Dictionary<Uri, BitmapImage>();

    public static readonly DependencyProperty SourceWithCustomRefererProperty =
        DependencyProperty.RegisterAttached(
            "SourceWithCustomReferer",
            typeof(Uri),
            typeof(ImageProperties),
            new PropertyMetadata(OnSourceWithCustomRefererChanged));

    private static void OnSourceWithCustomRefererChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        var image = (Image)o;
        var uri = (Uri)e.NewValue;

        if (DesignerProperties.IsInDesignTool)
        {
            // for the design surface we just load the image straight up
            image.Source = new BitmapImage(uri);
        }
        else
        {
            if (imageCache.ContainsKey(uri))
            {
                image.Source = imageCache[uri];
                return;
            }

            image.Source = null;

            HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;
            request.Headers["Referer"] = "http://www.WEBSITE.com"; // or your custom referer string here
            request.BeginGetResponse((result) =>
            {
                try
                {
                    Stream imageStream = request.EndGetResponse(result).GetResponseStream();
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        BitmapImage bitmapImage = new BitmapImage();
                        bitmapImage.CreateOptions = BitmapCreateOptions.BackgroundCreation;
                        bitmapImage.SetSource(imageStream);
                        image.Source = bitmapImage;
                        imageCache.Add(uri, bitmapImage);
                    });
                }
                catch (WebException)
                {
                    // add error handling
                }
            } , null);
        }
    }

    public static Uri GetSourceWithCustomReferer(Image image)
    {
        if (image == null)
        {
            throw new ArgumentNullException("Image");
        }
        return (Uri)image.GetValue(SourceWithCustomRefererProperty);
    }

    public static void SetSourceWithCustomReferer(Image image, Uri value)
    {
        if (image == null)
        {
            throw new ArgumentNullException("Image");
        }
        image.SetValue(SourceWithCustomRefererProperty, value);
    }
    #endregion
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多