2

我正在尝试修改此项目以在目录中显示图像。但问题是该代码不适用于像这样的所有图像。所以问题是

BitmapFrame bitmapFrame = BitmapFrame.Create(new Uri(path))

在存储库中,某些图像的BitmapFrame.Thumbnail属性为 null。我没有发现这些图像有什么问题。

如何使其适用于所有图像?

工作示例 不工作示例工作示例 不工作的例子

4

2 回答 2

2

您可以使用以下方法为没有缩略图的图像创建缩略图。

private BitmapSource CreateThumbnail(string path)
{
    BitmapImage bmpImage = new BitmapImage();
    bmpImage.BeginInit();
    bmpImage.UriSource = new Uri(path);
    bmpImage.DecodePixelWidth = 120;
    // bmpImage.DecodePixelHeight = 120; // alternatively, but not both
    bmpImage.EndInit();
    return bmpImage;
}
于 2018-03-01T06:08:45.673 回答
0

我在 SDK 示例中遇到了同样的问题。一些 jpg 显示为一个小的白色矩形,而不是缩略图。也许这是 JPG 格式不受支持的结果,或者 JPG 的标头中不包含 EXIF 信息?我不确定..我可以用 Raviraj 提供的程序解决它。

但是,Raviray 提供的答案有点短。只有当函数的结果被传递到图像类的 BitmapFrame 构造函数时,缩略图才有效。BitmapFrame 类有一个带有两个参数的构造函数,第二个是缩略图位图,请参阅How to override(use) BitmapFrame.Thumbnail property in WPF C#?

我让它与“坏”的 jpg 一起工作,在 SDK 示例中更改 Photo.cs,如下所示..

    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);
    }
于 2021-12-19T23:43:08.570 回答