简体   繁体   English

图像DecodeFailed事件未触发?

[英]Image DecodeFailed event not firing?

I'm trying to correctly load an image: testing right now against common errors (ie a file that is badly formatted). 我正在尝试正确加载图像:现在针对常见错误(即格式错误的文件)进行测试。 It is a currently a simple wpf application I use to test things. 这是一个当前用于测试事物的简单wpf应用程序。

public partial class MainWindow : Window
{
    public MainWindow() {
        var s = new BitmapImage();
        var uri = new Uri("test.txt", UriKind.RelativeOrAbsolute); //test exists but is obviously no image data
        DownloadImageListener dl = new DownloadImageListener(s);
        s.DecodeFailed += (sender, e) =>
        {
            Console.WriteLine("event is performed as lambda");
        };
        s.BeginInit();
        s.UriSource = uri;
        s.EndInit();
        Console.WriteLine(System.IO.File.Exists(uri.OriginalString)); //True!
        Console.WriteLine(s.IsDownloading); //"False" - done loading!
        Console.WriteLine(s.Width); //just to fail hard
    }
}

class DownloadImageListener
{
    private BitmapImage Img;

    public DownloadImageListener(BitmapImage i) {
        Img = i;
        // Add "ListChanged" to the Changed event on "List".
        Img.DecodeFailed += new EventHandler<ExceptionEventArgs>(ImageLoadFailed);
    }

    // This will be called whenever the list changes.
    private void ImageLoadFailed(object sender, EventArgs e) {
        Console.WriteLine("This is called when the loading failes");
    }

    public void Detach() {
        // Detach the event and delete the list
        Img.DecodeFailed -= new EventHandler<ExceptionEventArgs>(ImageLoadFailed);
        Img = null;
    }
}

The ImageLoadFailed method is never called (no line is printed nor does visual studio trigger the breakpoint I placed there). 永远不会调用ImageLoadFailed方法(不打印任何行,Visual Studio也不触发我在此处放置的断点)。 Am I doing something "wrong"? 难道我做错了什么”? I believe I followed the tutorial provided by msdn ? 我相信我遵循了msdn提供的教程吗?

EDIT: To rule out all potential other errors, I've added above the "isdownloading" check 编辑:为了排除所有潜在的其他错误,我在“正在下载”检查上方添加了

Console.WriteLine(System.IO.File.Exists(uri.OriginalString));
  • which shows "True" I've also added a lambda as listener - as shown by this page. 其中显示“ True”,我还添加了一个lambda作为侦听器-如本页所示

EDIT 2: 编辑2:

Testing "all" events it seems that only the "changed" event fires (so the code to catch the events is apparently correct) - the rest of the events never fire. 测试“所有”事件似乎仅触发“已更改”事件(因此捕获事件的代码显然是正确的)-其余事件永远不会触发。 - Why is this? - 为什么是这样?

DownloadFailed as it's name denotes will be executed only if the image can't be downloaded, and as you state in the comments it exists but it's not an image. 如名称所示, DownloadFailed仅在无法下载图像时才执行,并且如您在注释中所述,它存在但不是图像。

If you want to detect an error in the downloaded file then use the DecodeFailed event. 如果要在下载的文件中检测到错误,请使用DecodeFailed事件。

You could simply set BitmapCacheOption.OnLoad to make WPF immediately load the image file, and get an exception when it can't be decoded: 您可以简单地设置BitmapCacheOption.OnLoad来使WPF立即加载图像文件,并在无法解码时获取异常:

var bitmap = new BitmapImage();
try
{
    bitmap.BeginInit();
    bitmap.UriSource = new Uri("test.txt", UriKind.RelativeOrAbsolute);
    bitmap.CacheOption = BitmapCacheOption.OnLoad;
    bitmap.EndInit();
}
catch (Exception ex)
{
    Debug.WriteLine(ex.Message);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM