WPF中BitmapFrame和BitmapImage之间的区别

Kri*_*son 12 .net wpf

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

Fra*_*ger 12

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

实现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);
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)