-api-id | -api-type |
---|---|
T:Windows.Graphics.Imaging.BitmapEncoder |
winrt class |
Contains methods to create, edit and save images.
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.
Windows version | SDK version | Value added |
---|---|---|
1809 | 17763 | HeifEncoderId |
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();
}
}
}
Imaging, Camera resolution sample (Windows 10), Basic camera app sample (Windows 10), Video stabilization sample (Windows 10), Camera face detection sample (Windows 10), Manual camera controls sample (Windows 10), High dynamic range sample (Windows 10), Camera Advanced Capture sample