Difference between a BitmapFrame and BitmapImage in WPF
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);
The accepted answer is incomplete (not to suggest that my answer is complete either) and my addition may help someone somewhere.
The reason (albeit the only reason) I use a BitmapFrame is when I access the individual frames of a multiple-framed TIFF image using the TiffBitmapDecoder
class. For example,
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 is a low level primitive for image manipulation. It is usually used when you want to encode/decode some image from one format to another.
BitmapImage is more high level abstraction that has some neat data-binding properties (UriSource, etc).
If you are just displaying images and want some fine tuning BitmapImage is what you need.
If you are doing low level image manipulation then you will need BitmapFrame.