WPF中BitmapFrame和BitmapImage之间的区别

时间:2008-09-30 22:33:08

标签: .net wpf

WPF中的BitmapFrame和BitmapImage有什么区别?你会在哪里使用(即为什么你会使用BitmapFrame而不是BitmapImage?)

3 个答案:

答案 0 :(得分:12)

如果您需要获取位,您应该坚持使用抽象类BitmapSource,如果您只想绘制它,甚至可以ImageSource

实现BitmapFrame只是实现的面向对象性质。您不应该真正需要区分实现。 BitmapFrames可能包含一些额外的信息(元数据),但通常只有成像应用程序才会关注。

你会注意到从BitmapSource继承的其他类:

  • BitmapFrame
  • 的BitmapImage
  • CachedBitmap
  • ColorConvertedBitmap
  • CroppedBitmap
  • FormatConvertedBitmap
  • RenderTargetBitmap
  • TransformedBitmap
  • WriteableBitmap的

您可以通过构造BitmapImage对象从URI获取BitmapSource:

Uri uri = ...;
BitmapSource bmp = new BitmapImage(uri);
Console.WriteLine("{0}x{1}", bmp.PixelWIdth, bmp.PixelHeight);

BitmapSource也可以来自解码器。在这种情况下,您间接使用BitmapFrame。

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

答案 1 :(得分:2)

接受的答案是不完整的(不是暗示我的答案也是完整的),而且我的补充可能会帮助某个人。

我使用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)
}

答案 2 :(得分:0)

BitmapFrame是用于图像处理的低级原语。当您想要将某些图像从一种格式编码/解码到另一种格式时,通常会使用它。

BitmapImage是更高级的抽象,具有一些整洁的数据绑定属性(UriSource等)。

如果您只是在显示图像并希望进行一些微调,那么BitmapImage就是您所需要的。

如果您正在进行低级图像处理,那么您将需要BitmapFrame。