共用方式為


如何寫入影像中繼資料 (HTML)

[ 本文的目標對象是撰寫 Windows 執行階段 App 的 Windows 8.x 和 Windows Phone 8.x 開發人員。如果您正在開發適用於 Windows 10 的 App,請參閱 最新文件 ]

本主題說明如何使用 BitmapEncoder 物件寫入影像中繼資料。 您可以使用 Windows 屬性或 Windows 影像處理元件 (WIC) 中繼資料查詢語言來撰寫中繼資料。

如需使用 BitmapDecoderBitmapEncoder 的中繼資料存取的詳細資訊,請參閱如何讀取影像中繼資料

注意  您可以使用 Windows.Storage.FileProperties API 取得並設定 Windows.StorageFile 上的基本屬性,不需要開啟資料流。如需詳細資訊,請參閱如何取得影像屬性

 

您必須知道的事

技術

先決條件

指示

步驟 1: 取得編碼器物件

撰寫開始函式,接收 BitmapEncoder 物件。

function (encoder) {
    

編碼器可讓您存取影像中繼資料。 如果您還沒有編碼器物件,請參閱如何編碼新影像

步驟 2: 建立要設定的中繼資料集合

使用 BitmapPropertySet 儲存您要在編碼器上設定的中繼資料項目。每個中繼資料項目都是一個機碼值組。

機碼是一個字串,可識別要設定的中繼資料項目。BitmapEncoder 接受某些使用 WIC 中繼資料查詢語言所建構的 Windows 屬性及查詢。如需支援的 Windows 屬性清單,請參閱支援的 Windows 屬性。如需支援的 WIC 中繼資料查詢摘要,請參閱 WIC 原生影像格式中繼資料查詢

值是一個 BitmapTypedValue,可讓您將實際中繼資料值與明確資料類型 (Windows.Foundation.PropertyType) 產生關聯。

將指定 EXIF 方向的 System.Photo.Orientation 中繼資料值設為 1,這會指定「正常」方向,再指派 uint16 資料類型。

    var propertySet = new Windows.Graphics.Imaging.BitmapPropertySet();
    var orientationValue = new Windows.Graphics.Imaging.BitmapTypedValue(
        1, // Defined as EXIF orientation = "normal"
        Windows.Foundation.PropertyType.uint16
        );

    propertySet.insert("System.Photo.Orientation", orientationValue);     

步驟 3: 在編碼器上設定中繼資料

所有中繼資料項目都建構完成之後,可在編碼器上設定項目,然後繼續編碼作業。

    encoder.bitmapProperties.setPropertiesAsync(propertySet).done(function () {
        // Continue the encoding operation.
    }, function (error) {
        switch (error.number) {
            case -2003292351: // WINCODEC_ERR_PROPERTYNOTSUPPORTED
                // The file format does not support this property.
                break;
            default:
                throw error;
        }
    });
}    

注意  每個影像格式都支援不同組的中繼資料項目。如果您嘗試設定影像格式不支援的中繼資料或屬性項目,就會收到錯誤。例如,只有 JPEG、TIFF 及 JPEG-XR 影像可支援 System.Photo.Orientation 屬性。

 

備註

設定多個屬性和設定單一屬性相同,只是您要在清單中指定多個名稱/值組。 這裡的程式碼會同時設定 OrientationCameraModel

encoder.savePropertiesAsync(["System.Photo.Orientation": 1],
                                                         "System.Photo.CameraModel": "Camera Model 1"]);

相關主題

如何編碼影像

System.Photo 屬性

如何讀取影像中繼資料

WIC 中繼資料查詢語言

WIC 原生影像格式中繼資料查詢

支援的 Windows 屬性