简体   繁体   中英

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.

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). Am I doing something "wrong"? I believe I followed the tutorial provided by 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.

EDIT 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.

If you want to detect an error in the downloaded file then use the DecodeFailed event.

You could simply set BitmapCacheOption.OnLoad to make WPF immediately load the image file, and get an exception when it can't be decoded:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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