问题 WPF中BitmapFrame和BitmapImage之间的区别


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


7750
2017-09-30 22:33


起源



答案:


你应该坚持使用抽象类 的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也可以来自解码器。在这种情况下,您间接使用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);

12
2017-09-30 22:43



你如何使用ImageSource?如何使用Uri加载BitmapSource? - Kris Erickson


答案:


你应该坚持使用抽象类 的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也可以来自解码器。在这种情况下,您间接使用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);

12
2017-09-30 22:43



你如何使用ImageSource?如何使用Uri加载BitmapSource? - Kris Erickson


我知道这是一个古老的问题,但接受的答案是不完整的(不是暗示我的答案也是完整的),而且我的补充可能会帮助某个人。

原因(尽管如此) 只要 原因)我使用BitmapFrame时,我使用的方式访问多帧TIFF图像的各个帧 TiffBitmapDecoder 类。例如,

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

1
2017-07-25 00:45





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

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

如果您只是显示图像并想要一些微调BitmapImage就是您所需要的。

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


0
2017-09-30 22:55