Difference between a BitmapFrame and BitmapImage in WPF

前端 未结 3 1501
情话喂你
情话喂你 2021-02-13 22:03

What is the difference between a BitmapFrame and BitmapImage in WPF? Where would you use each (ie. why would you use a BitmapFrame rather than a BitmapImage?)

3条回答
  •  再見小時候
    2021-02-13 22:14

    You should stick to using the abstract class BitmapSource if you need to get at the bits, or even ImageSource if you just want to draw it.

    The implementation BitmapFrame is just the object oriented nature of the implementation showing through. You shouldn't really have any need to distinguish between the implementations. BitmapFrames may contain a little extra information (metadata), but usually nothing but an imaging app would care about.

    You'll notice these other classes that inherit from BitmapSource:

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

    You can get a BitmapSource from a URI by constructing a BitmapImage object:

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

    The BitmapSource could also come from a decoder. In this case you are indirectly using 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);
    

提交回复
热议问题