0

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;
}
  • As a note, if you have already encoded a bitmap frame into a MemoryStream, there is no need at all to write that to a file and decode another image from that file. Just decode directly from the MemoryStream by setting the new BitmapImage's StreamSource property like `tmpImage.StreamSource = memoryStream;`. Do not forget to rewind the stream beforehand, e.g. by setting its Position to zero. – Clemens Aug 28 '19 at 10:18
  • I also doubt that creating these thumbnails is useful at all. Since you have already decoded the full-size frame, you could simply show a TransformedBitmap with a ScaleTransform. – Clemens Aug 28 '19 at 10:23
  • Thanks, for your comment, I will try to change the method, using TransformedBitmap, etc. – Ivan Raskatov Aug 28 '19 at 13:24

1 Answers1

0

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);
    }
Goodies
  • 1,951
  • 21
  • 26