microsoft / Windows-appsample-photo-editor

Photo Editor is a UWP photo viewing and editing sample that highlights development with C++/WinRT.
MIT License
182 stars 63 forks source link

GetImageThumbnailAsync null reference exception #4

Open Swifter opened 6 years ago

Swifter commented 6 years ago

photoeditor_exception_when_scrolling

GetTumbnailAsync sometimes returns NULL, which your code does not account for. Do this instead:

    IAsyncOperation<BitmapImage> Photo::GetImageThumbnailAsync() const
    {
        auto thumbnail = co_await m_imageFile.GetThumbnailAsync(FileProperties::ThumbnailMode::PicturesView);
        BitmapImage bitmapImage{};

        // fix for when thumbnail is NULL
        if (NULL != thumbnail)
        {
            bitmapImage.SetSource(thumbnail);
            thumbnail.Close();
        }

        co_return bitmapImage;
    }
Swifter commented 4 years ago

GetThumbnailAsync returns NULL when you attempt to open a file that looks to the file system like it might be a picture (it has .jpg or .png extension), but is actually not (ex: it is a text file someone renamed to .jpg).