簡體   English   中英

如何在 WPF C# 中覆蓋(使用)BitmapFrame.Thumbnail 屬性?

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

你好! 問題是? 我有一個多頁 Tiff 文件要顯示,並且我使用 BitmapFrame.Thumbnail 屬性來顯示我的多頁 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 並使用以下方法創建 BitmapFrames 列表:

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 屬性為 null

我使用該主題中提供的函數,使用 Ivan 對構造函數的調用,使用兩個bitmapsource 參數使其工作。

我現在使用的 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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM