BitmapEncoder Class

Definition

Contains methods to create, edit and save images.

public ref class BitmapEncoder sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class BitmapEncoder final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public sealed class BitmapEncoder
Public NotInheritable Class BitmapEncoder
Inheritance
Object Platform::Object IInspectable BitmapEncoder
Attributes

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)

Examples

Here's a partial example of creating an encoder object. This example assumes you selected a file with Windows.Storage.Pickers.FileSavePicker. For full instructions on selecting a file, creating an encoder, and encoding an image see Imaging

private async void SaveSoftwareBitmapToFile(SoftwareBitmap softwareBitmap, StorageFile outputFile)
{
    using (IRandomAccessStream stream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
    {
        // Create an encoder with the desired format
        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);

        // Set the software bitmap
        encoder.SetSoftwareBitmap(softwareBitmap);

        // Set additional encoding parameters, if needed
        encoder.BitmapTransform.ScaledWidth = 320;
        encoder.BitmapTransform.ScaledHeight = 240;
        encoder.BitmapTransform.Rotation = Windows.Graphics.Imaging.BitmapRotation.Clockwise90Degrees;
        encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant;
        encoder.IsThumbnailGenerated = true;

        try
        {
            await encoder.FlushAsync();
        }
        catch (Exception err)
        {
            const int WINCODEC_ERR_UNSUPPORTEDOPERATION = unchecked((int)0x88982F81);
            switch (err.HResult)
            {
                case WINCODEC_ERR_UNSUPPORTEDOPERATION: 
                    // If the encoder does not support writing a thumbnail, then try again
                    // but disable thumbnail generation.
                    encoder.IsThumbnailGenerated = false;
                    break;
                default:
                    throw;
            }
        }

        if (encoder.IsThumbnailGenerated == false)
        {
            await encoder.FlushAsync();
        }


    }
}

Remarks

Image formats

BitmapEncoder can encode the following formats.

  • JPEG
  • PNG
  • GIF
  • TIFF
  • BMP
  • JPEG-XR

For a list of decoding formats, see the BitmapDecoder topic.

BitmapEncoder behaves differently from BitmapDecoder in that it doesn't provide random access to the frames in an image. Instead, you need to perform actions on the encoder in a specific order.

When you create a BitmapEncoder, it provides access to data on the container and the first frame. When you are done setting data on the first frame and container, if you don't want to encode any additional frames, then call FlushAsync to complete the encoding operation.

If you want to encode an additional frame, call GoToNextFrameAsync. This commits the data in the container and the first frame so you can't edit them anymore. At this point any actions you perform on the encoder will affect the second frame. After you are done with each frame, you can call GoToNextFrameAsync to commit and append a new frame, or call FlushAsync to finish.Bitmap encoders may expose various encoding options that affect the quality, size and other properties of the encoded output file. For more info, see Imaging.

Version history

Windows version SDK version Value added
1809 17763 HeifEncoderId

Properties

BitmapContainerProperties

The metadata for the container.

BitmapProperties

The metadata for the selected frame.

BitmapTransform

A BitmapTransform object that is used to specify how the frame bitmap is to be transformed.

BmpEncoderId

The unique identifier of the built-in BMP encoder.

EncoderInformation

Information about the bitmap encoder.

GeneratedThumbnailHeight

The height, in pixels, of any generated thumbnail.

GeneratedThumbnailWidth

The width, in pixels, of any generated thumbnail.

GifEncoderId

The unique identifier of the built-in GIF encoder.

HeifEncoderId

The unique identifier of the built-in HEIF encoder.

IsThumbnailGenerated

Indicates whether or not a new thumbnail is automatically generated.

JpegEncoderId

The unique identifier of the built-in JPEG encoder.

JpegXREncoderId

The unique identifier of the built-in JPEG-XR encoder.

PngEncoderId

The unique identifier of the built-in PNG encoder.

TiffEncoderId

The unique identifier of the built-in TIFF encoder.

Methods

CreateAsync(Guid, IRandomAccessStream)

Asynchronously creates a new BitmapEncoder.

CreateAsync(Guid, IRandomAccessStream, IIterable<KeyValuePair<String,BitmapTypedValue>>)

Asynchronously creates a new BitmapEncoder for the specified codec with the specified encoding options and initializes it on a stream.

CreateForInPlacePropertyEncodingAsync(BitmapDecoder)

Asynchronously creates a new BitmapEncoder for in-place property and metadata editing. The new encoder can only edit bitmap properties in-place and will fail for any other uses.

CreateForTranscodingAsync(IRandomAccessStream, BitmapDecoder)

Asynchronously creates a new BitmapEncoder and initializes it using data from an existing BitmapDecoder.

FlushAsync()

Asynchronously commits and flushes all of the image data.

GetEncoderInformationEnumerator()

A list of the bitmap encoders installed on the system and information about them.

GoToNextFrameAsync()

Asynchronously commits the current frame data and appends a new empty frame to be edited.

GoToNextFrameAsync(IIterable<KeyValuePair<String,BitmapTypedValue>>)

Asynchronously commits the current frame data and appends a new empty frame, with the specified encoding options, to be edited.

SetPixelData(BitmapPixelFormat, BitmapAlphaMode, UInt32, UInt32, Double, Double, Byte[])

Sets pixel data on the frame.

SetSoftwareBitmap(SoftwareBitmap)

Sets the image data of the current frame using the specified SoftwareBitmap.

Applies to

See also