在WPF中,BitmapFrame和BitmapImage有什么区别?何时使用它们(即为什么会使用BitmapFrame而不是BitmapImage)?
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。