Bitmap Encoders and Decoders

A bitmap encoder is a class that knows how to generate a bitmap stream in a particular format. WPF provides encoders for all the image formats listed earlier. Encoders are named after their format (e.g., PngBitmapEncoder, JpegBitmapEncoder, etc.).

Example 13-35 shows how to write a bitmap out to disk as a JPEG file. This function works with any BitmapSource.

Example 13-35. Creating a JPEG file static void WriteJpeg(string fileName, int quality, BitmapSource bmp) {

JpegBitmapEncoder encoder = new JpegBitmapEncoder(); BitmapFrame outputFrame = BitmapFrame.Create(bmp); encoder.Frames.Add(outputFrame); encoder.OualityLevel = quality;

using (FileStream file = File.OpenWrite(fileName)) { encoder.Save(file);

Decoders work in the opposite direction—they know how to read bitmap streams of a particular format. Decoders are used implicitly whenever you load a bitmap stream into a BitmapImage, but you can also use them explicitly. This is necessary if you wish to access bitmap metadata or retrieve all the frames in a multiframe image file.

Example 13-36 shows how to load a JPEG image with a decoder to discover the type of camera used to take the image.

Example 13-36. Reading bitmap metadata static string GetCamera(string myJpegPath) {

JpegBitmapDecoder decoder = new JpegBitmapDecoder(new Uri(myJpegPath),

BitmapCreateOptions.None, BitmapCacheOption.None); BitmapMetadata bmpData = (BitmapMetadata) decoder.Frames[0].Metadata; return bmpData.CameraModel;

Notice that both the encoder and the decoder have a property called Frames to represent the frames of the image. For single-frame formats, this cannot contain more than one frame, but an animated GIF would contain multiple frames.

Metadata is returned only at the level of individual frames. The decoder classes all offer a Metadata property, but it is always null for all of the decoders that ship as part of the first release of WPF. Use the Metadata property of the frame instead.

There is one last bitmap feature we will examine. It is a little different from the rest, because it allows bitmap processing to be applied to any part of the UI, not just bitmaps.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment