简体   繁体   中英

How to override(use) BitmapFrame.Thumbnail property in WPF C#?

Hello! The problem is? that I've got a multipage Tiff file to show, and I use BitmapFrame.Thumbnail property to show small size thumbnail of every frame(page) of my multipage Tiff file. But< for some reason? the property returns null. Please, give step by step description, of how this should be done?

I've already tried to create my own BitmapSource thumbnail with this method:

public static BitmapImage GetThumbnail(BitmapFrame bitmapFrame)
        {
            try
            {
                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                MemoryStream memorystream = new MemoryStream();
                BitmapImage tmpImage = new BitmapImage();
                encoder.Frames.Add(bitmapFrame);
                encoder.Save(memorystream);
                tmpImage.BeginInit();
                tmpImage.CacheOption = BitmapCacheOption.OnLoad;
                tmpImage.StreamSource = new MemoryStream(memorystream.ToArray());
                File.WriteAllBytes( $"{Path.GetTempFileName()}.jpg", memorystream.ToArray());
                tmpImage.UriSource = new Uri($"{Path.GetTempFileName()}.jpg");
                tmpImage.DecodePixelWidth = 80;
                tmpImage.DecodePixelHeight = 120;
                tmpImage.EndInit();
                memorystream.Close();
                return tmpImage;
            }
            catch (Exception ex)
            {
                return null;
                throw ex;
            }
        } 

then I convert the result to BitmapSource and create a list of BitmapFrames using:

List<BitmapFrame> tiffImageList = new List<BitmapFrame>();
tiffImageList.Add(new TiffImage() { index = imageIndex, image = BitmapFrame.Create(frame, (BitmapSource)GetThumbnail(frame))});

In the end I try to get property, but it returns null:

foreach (var tiffImage in tiffImageList)
{
   Image image = new Image();
   image.Source = tiffImage.image.Thumbnail;
}

I ran into a similar issue, modifying with the SDK PhotoViewerDemo example. Some valid jpg's are shown as white square thumbnails.

I think I found why the question code does not work. Ivan's question provides a correct constructor of BitmapFrame, but the functions have to create a BitmapSource, not a BitmapImage.

C# BitmapFrame.Thumbnail property is null for some images

I got it working with the function provided in that topic, using Ivan's call to the constructor, using the two bitmapsource arguments.

Code in the SDK example I now use is..

    private BitmapSource CreateBitmapSource(Uri path)
    {
        BitmapImage bmpImage = new BitmapImage();
        bmpImage.BeginInit();
        bmpImage.UriSource = path;
        bmpImage.EndInit();
        return bmpImage;
    }

    private BitmapSource CreateThumbnail(Uri path)
    {
        BitmapImage bmpImage = new BitmapImage();
        bmpImage.BeginInit();
        bmpImage.UriSource = path;
        bmpImage.DecodePixelWidth = 120;
        bmpImage.EndInit();
        return bmpImage;
    }

    // it has to be plugged in here,
    public Photo(string path)
    {
        Source = path;
        _source = new Uri(path);
        // replaced.. Image = BitmapFrame.Create(_source);
        // with this:
        Image = BitmapFrame.Create(CreateBitmapSource(_source),CreateThumbnail(_source));
        Metadata = new ExifMetadata(_source);
    }

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