【发布时间】:2008-09-30 22:33:07
【问题描述】:
WPF 中的 BitmapFrame 和 BitmapImage 有什么区别?您将在哪里使用每个(即。为什么要使用 BitmapFrame 而不是 BitmapImage?)
【问题讨论】:
WPF 中的 BitmapFrame 和 BitmapImage 有什么区别?您将在哪里使用每个(即。为什么要使用 BitmapFrame 而不是 BitmapImage?)
【问题讨论】:
你应该坚持使用抽象类BitmapSource,如果你需要了解它,甚至是ImageSource,如果你只是想画它。
BitmapFrame 的实现只是实现的面向对象的本质。您真的不需要区分实现。 BitmapFrames 可能包含一些额外的信息(元数据),但通常只有图像应用会关心。
您会注意到这些继承自 BitmapSource 的其他类:
您可以通过构造 BitmapImage 对象从 URI 中获取 BitmapSource:
Uri uri = ...;
BitmapSource bmp = new BitmapImage(uri);
Console.WriteLine("{0}x{1}", bmp.PixelWIdth, bmp.PixelHeight);
BitmapSource 也可以来自解码器。在这种情况下,您间接使用了 BitmapFrames。
Uri uri = ...;
BitmapDecoder dec = BitmapDecoder.Create(uri, BitmapCreateOptions.None, BitmapCacheOption.Default);
BitmapSource bmp = dec.Frames[0];
Console.WriteLine("{0}x{1}", bmp.PixelWIdth, bmp.PixelHeight);
【讨论】:
接受的答案是不完整的(也不意味着我的答案是完整的),我的补充可能会帮助某个地方的人。
我使用 BitmapFrame 的原因(尽管是唯一原因)是当我使用 TiffBitmapDecoder 类访问多帧 TIFF 图像的各个帧时。例如,
TiffBitmapDecoder decoder = new TiffBitmapDecoder(
new Uri(filename),
BitmapCreateOptions.None,
BitmapCacheOption.None);
for (int frameIndex = 0; frameIndex < decoder.Frames.Count; frameIndex++)
{
BitmapFrame frame = decoder.Frames[frameIndex];
// Do something with the frame
// (it inherits from BitmapSource, so the options are wide open)
}
【讨论】:
BitmapFrame 是用于图像处理的低级原语。当您想将某些图像从一种格式编码/解码为另一种格式时,通常会使用它。
BitmapImage 是更高级别的抽象,具有一些简洁的数据绑定属性(UriSource 等)。
如果您只是显示图像并且想要一些微调 BitmapImage 是您所需要的。
如果您正在执行低级别的图像处理,那么您将需要 BitmapFrame。
【讨论】: