如何重写(使用)WPF C#中的BitmapFrame.Thumbail属性? [英] How to override(use) BitmapFrame.Thumbnail property in WPF C#?

查看:104
本文介绍了如何重写(使用)WPF C#中的BitmapFrame.Thumbail属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!问题是?我有一个多页的Tiff文件要展示,我使用 属性显示我的多页Tiff文件的每个框架(页面)的小尺寸缩略图。但是因为某些原因?该属性返回NULL。请一步一步地描述一下,应该如何做到这一点?

我已经尝试使用此方法创建我自己的BitmapSource缩略图:

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

然后我将结果转换为BitmapSource并使用以下命令创建一个BitmapFrame列表:

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

最后我尝试获取属性,但返回NULL:

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

推荐答案

我也遇到了类似的问题,修改了SDK PhotoViewerDemo示例。某些有效的jpg显示为白色正方形缩略图。

我想我找到了问题代码不起作用的原因。Ivan的问题提供了BitmapFrame的正确构造函数,但这些函数必须创建BitmapSource,而不是BitmapImage。

C# BitmapFrame.Thumbnail property is null for some images

我通过使用Ivan对构造函数的调用,使用两个位图源参数,使其与该主题中提供的函数一起工作。

我现在使用的SDK示例中的代码是..

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

这篇关于如何重写(使用)WPF C#中的BitmapFrame.Thumbail属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆