BitmapEncoder 類別

定義

包含建立、編輯及儲存影像的方法。

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
繼承
Object Platform::Object IInspectable BitmapEncoder
屬性

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)

範例

以下是建立編碼器物件的部分範例。 此範例假設您已選取 Windows.Storage.Pickers.FileSavePicker的檔案。 如需選取檔案、建立編碼器和編碼影像的完整指示,請參閱 映射處理

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();
        }


    }
}

備註

影像格式

BitmapEncoder 可以編碼下列格式。

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

如需解碼格式的清單,請參閱 BitmapDecoder 主題。

BitmapEncoder 的行為與 BitmapDecoder不同,因為它不會隨機存取影像中的畫面格。 相反地,您必須以特定順序在編碼器上執行動作。

當您建立 BitmapEncoder 時,它會提供容器和第一個框架上資料的存取權。 當您在第一個畫面格和容器上設定資料時,如果您不想編碼任何其他畫面格,請呼叫 FlushAsync 來完成編碼作業。

如果您想要編碼其他畫面格,請呼叫 GoToNextFrameAsync。 這會認可容器中的資料和第一個框架,因此您無法再編輯它們。 此時,您在編碼器上執行的任何動作都會影響第二個畫面。 完成每個畫面之後,您可以呼叫 GoToNextFrameAsync 來認可並附加新的框架,或呼叫 FlushAsync 來完成。點陣圖編碼器可能會公開會影響編碼輸出檔案品質、大小和其他屬性的各種編碼選項。 如需詳細資訊,請參閱 映射處理

版本歷程記錄

Windows 版本 SDK 版本 新增值
1809 17763 HeifEncoderId

屬性

BitmapContainerProperties

容器的中繼資料。

BitmapProperties

所選框架的中繼資料。

BitmapTransform

BitmapTransform物件,用來指定要如何轉換框架點陣圖。

BmpEncoderId

內建 BMP 編碼器的唯一識別碼。

EncoderInformation

點陣圖編碼器的相關資訊。

GeneratedThumbnailHeight

任何所產生縮圖的高度,以圖元為單位。

GeneratedThumbnailWidth

任何所產生縮圖的寬度,以圖元為單位。

GifEncoderId

內建 GIF 編碼器的唯一識別碼。

HeifEncoderId

內建 HEIF 編碼器的唯一識別碼。

IsThumbnailGenerated

指出是否自動產生新的縮圖。

JpegEncoderId

內建 JPEG 編碼器的唯一識別碼。

JpegXREncoderId

內建 JPEG-XR 編碼器的唯一識別碼。

PngEncoderId

內建 PNG 編碼器的唯一識別碼。

TiffEncoderId

內建 TIFF 編碼器的唯一識別碼。

方法

CreateAsync(Guid, IRandomAccessStream)

以非同步方式建立新的 BitmapEncoder

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

使用指定的編碼選項,以非同步方式為指定的編解碼器建立新的 BitmapEncoder ,並在資料流程上初始化它。

CreateForInPlacePropertyEncodingAsync(BitmapDecoder)

以非同步方式建立新的 BitmapEncoder 以進行就地屬性和中繼資料編輯。 新的編碼器只能就地編輯點陣圖屬性,而且任何其他用途都會失敗。

CreateForTranscodingAsync(IRandomAccessStream, BitmapDecoder)

以非同步方式建立新的 BitmapEncoder, 並使用來自現有 BitmapDecoder的資料初始化它。

FlushAsync()

以非同步方式認可並排清所有影像資料。

GetEncoderInformationEnumerator()

系統上所安裝的點陣圖編碼器清單及其相關資訊。

GoToNextFrameAsync()

以非同步方式認可目前的框架資料,並附加要編輯的新空白框架。

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

以非同步方式認可目前的框架資料,並附加新的空白框架,以及要編輯的指定編碼選項。

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

設定畫面上的圖元資料。

SetSoftwareBitmap(SoftwareBitmap)

使用指定的 SoftwareBitmap設定目前畫面的影像資料。

適用於

另請參閱