This documentation is archived and is not being maintained.

JpegBitmapEncoder Class

Defines an encoder that is used to encode Joint Photographics Experts Group (JPEG) format images.

Namespace:  System.Windows.Media.Imaging
Assembly:  PresentationCore (in PresentationCore.dll)

public sealed class JpegBitmapEncoder : BitmapEncoder
This managed class is not typically used in XAML.

Joint Photographics Experts Group (JPEG) format bitmaps support frame level thumbnails and frame level metadata. They do not support preview, global thumbnails, global metadata, or multiple frames.

Encoding does not work in partial trust. See Windows Presentation Foundation Partial Trust Security for information on partial trust.

The following examples show how to decode and encode a JPEG image using the specific JpegBitmapDecoder and JpegBitmapEncoder objects. For the complete sample, see JPEG Encoder and Decoder Sample.

This example demonstrates how to decode a JPEG image using a JpegBitmapDecoder from a FileStream.

// Open a Stream and decode a JPEG image
Stream imageStreamSource = new FileStream("tulipfarm.jpg", FileMode.Open, FileAccess.Read, FileShare.Read);
JpegBitmapDecoder decoder = new JpegBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];

// Draw the Image
Image myImage = new Image();
myImage.Source = bitmapSource;
myImage.Stretch = Stretch.None;
myImage.Margin = new Thickness(20);

This example demonstrates how to encode a BitmapSource into a JPEG image using a JpegBitmapEncoder.

int width = 128;
int height = width;
int stride = width / 8;
byte[] pixels = new byte[height * stride];

// Define the image palette
BitmapPalette myPalette = BitmapPalettes.Halftone256;

// Creates a new empty image with the pre-defined palette
BitmapSource image = BitmapSource.Create(

FileStream stream = new FileStream("new.jpg", FileMode.Create);
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
TextBlock myTextBlock = new TextBlock();
myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString();
encoder.FlipHorizontal = true;
encoder.FlipVertical = false;
encoder.QualityLevel = 30;
encoder.Rotation = Rotation.Rotate90;

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0