影像處理概觀

本主題提供 Microsoft Windows Presentation Foundation Imaging Component 的簡介。 WPF 映射可讓開發人員顯示、轉換和格式化影像。

WPF 影像處理元件

WPF 映射處理提供 Microsoft Windows 內映射處理功能的重要增強功能。 影像功能,例如在通用控制項上顯示點陣圖或使用影像,先前依賴 Microsoft Windows Graphics Device Interface (GDI) 或 Microsoft Windows GDI+ 程式庫。 這些 API 提供基準映射功能,但缺少支援編解碼器擴充性和高逼真度影像支援等功能。 WPF 映射處理的設計目的是要克服 GDI 和 GDI+ 的缺點,並提供一組新的 API 來顯示及使用應用程式內的影像。

有兩種方式可以存取 WPF 映射處理 API、受控元件和非受控元件。 Unmanaged 元件提供下列功能。

  • 新的或專屬影像格式的擴充性模型。

  • 改善原生圖像格式的效能和安全性,包括點陣圖(BMP)、聯合攝影專家組(JPEG)、可攜式網狀圖形(PNG)、標記圖像檔案格式(TIFF)、Microsoft Windows Media Photo、圖形交換格式(GIF)和圖示(.ico)。

  • 高位元深度影像資料最多可保留每色頻 8 位元 (每像素 32 位元)。

  • 非破壞性的影像縮放、裁剪及旋轉。

  • 簡化的色彩管理。

  • 支援檔案內、專屬中繼資料。

  • 受控元件會利用非受控基礎結構來提供影像與其他 WPF 功能的無縫整合,例如使用者介面(UI)、動畫和圖形。 受管理元件也受益于 Windows Presentation Foundation (WPF) 映射編解碼器擴充性模型,可自動辨識 WPF 應用程式中的新影像格式。

大部分受控 WPF 映射處理 API 都位於 命名空間中 System.Windows.Media.Imaging ,不過有數個重要類型,例如 ImageBrushImageDrawing 位於 System.Windows.Media 命名空間中,且 Image 位於 命名空間中 System.Windows.Controls

本主題提供 Managed 元件的詳細資訊。 如需 Unmanaged API 的詳細資訊,請參閱 Unmanaged WPF 映射元件 檔。

WPF 影像格式

轉碼器可以用來將特定媒體格式解碼或編碼。 WPF 映射包含 BMP、JPEG、PNG、TIFF、Windows 媒體相片、GIF 和 ICON 影像格式的編解碼器。 每個轉碼器都可以讓應用程式解碼和編碼各自的影像格式 (但 ICON 在編碼部分是例外)。

BitmapSource 是影像解碼和編碼中所使用的重要類別。 它是 WPF 映射處理管線的基本建置組塊,代表特定大小和解析度的單一常數圖元集。 BitmapSource可以是多個畫面影像的個別畫面格,或者可能是在 上 BitmapSource 執行的轉換結果。 它是 WPF 映射中使用的許多主要類別的父代,例如 BitmapFrame

BitmapFrame用來儲存影像格式的實際點陣圖資料。 許多影像格式只支援單 BitmapFrame 一 ,不過 GIF 和 TIFF 等格式支援每個影像的多個畫面格。 畫面格會由解碼器作為輸入資料並傳遞至編碼器以建立影像檔。

下列範例示範如何 BitmapFrameBitmapSource 建立 ,然後將 新增至 TIFF 映射。

BitmapSource image5 = BitmapSource.Create(
    width,
    height,
    96,
    96,
    PixelFormats.Indexed1,
    BitmapPalettes.WebPalette,
    pixels,
    stride);

FileStream stream5 = new FileStream("palette.tif", FileMode.Create);
TiffBitmapEncoder encoder5 = new TiffBitmapEncoder();
encoder5.Frames.Add(BitmapFrame.Create(image5));
encoder5.Save(stream5);
Dim image5 As BitmapSource = System.Windows.Media.Imaging.BitmapSource.Create(width, height, 96, 96, PixelFormats.Indexed1, BitmapPalettes.WebPalette, pixels, stride)

Dim stream5 As New FileStream("palette.tif", FileMode.Create)
Dim encoder5 As New TiffBitmapEncoder()
encoder5.Frames.Add(BitmapFrame.Create(image5))
encoder5.Save(stream5)

影像格式解碼

影像解碼是指將影像格式變成系統可用之影像資料的轉譯作業。 接著可使用影像資料來顯示、處理,或編碼成不同格式。 選取的解碼器是根據影像格式而定。 除非指定特定的解碼器,否則會自動選取轉碼器。 在 WPF 中顯示影像一節中的範例示範自動解碼。 使用 Unmanaged WPF Imaging 介面開發的自訂格式解碼器,並向系統註冊時,會自動參與解碼器選取。 這可讓自訂格式自動顯示在 WPF 應用程式中。

下列範例示範如何使用點陣圖解碼器來解碼 BMP 格式影像。


// Open a Uri and decode a BMP image
System::Uri^ myUri = gcnew System::Uri("tulipfarm.bmp", UriKind::RelativeOrAbsolute);
BmpBitmapDecoder^ decoder2 = gcnew BmpBitmapDecoder(myUri, BitmapCreateOptions::PreservePixelFormat, BitmapCacheOption::Default);
BitmapSource^ bitmapSource2 = decoder2->Frames[0];

// Draw the Image
Image^ myImage2 = gcnew Image();
myImage2->Source = bitmapSource2;
myImage2->Stretch = Stretch::None;
myImage2->Margin = System::Windows::Thickness(20);

// Open a Uri and decode a BMP image
Uri myUri = new Uri("tulipfarm.bmp", UriKind.RelativeOrAbsolute);
BmpBitmapDecoder decoder2 = new BmpBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource2 = decoder2.Frames[0];

// Draw the Image
Image myImage2 = new Image();
myImage2.Source = bitmapSource2;
myImage2.Stretch = Stretch.None;
myImage2.Margin = new Thickness(20);
' Open a Uri and decode a BMP image
Dim myUri As New Uri("tulipfarm.bmp", UriKind.RelativeOrAbsolute)
Dim decoder2 As New BmpBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default)
Dim bitmapSource2 As BitmapSource = decoder2.Frames(0)

' Draw the Image
Dim myImage2 As New Image()
myImage2.Source = bitmapSource2
myImage2.Stretch = Stretch.None
myImage2.Margin = New Thickness(20)

影像格式編碼

影像編碼是指將影像資料變成特定影像格式的轉換作業。 接著可使用已編碼的影像資料建立新的影像檔。 WPF 映射處理會針對上述每個影像格式提供編碼器。

下列範例示範使用編碼器儲存新建立的點陣圖影像。

FileStream^ stream = gcnew FileStream("new.bmp", FileMode::Create);
BmpBitmapEncoder^ encoder = gcnew BmpBitmapEncoder();
TextBlock^ myTextBlock = gcnew TextBlock();
myTextBlock->Text = "Codec Author is: " + encoder->CodecInfo->Author->ToString();
encoder->Frames->Add(BitmapFrame::Create(image));
encoder->Save(stream);
FileStream stream = new FileStream("new.bmp", FileMode.Create);
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
TextBlock myTextBlock = new TextBlock();
myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString();
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);
Dim stream As New FileStream("new.bmp", FileMode.Create)
Dim encoder As New BmpBitmapEncoder()
Dim myTextBlock As New TextBlock()
myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString()
encoder.Frames.Add(BitmapFrame.Create(image))
encoder.Save(stream)

在 WPF 中顯示影像

有數種方式可以在 Windows Presentation Foundation (WPF) 應用程式中顯示影像。 影像可以使用 控制項來顯示 Image 、使用 ImageBrush 繪製在視覺效果上,或使用 繪製 ImageDrawing

使用影像控制項

Image 是架構元素,也是在應用程式中顯示影像的主要方式。 在 XAML 中, Image 可以使用兩種方式;屬性語法或屬性語法。 下列範例顯示如何使用屬性 (Attribute) 語法和屬性 (Property) 標記語法呈現 200 像素寬的影像。 如需有關屬性 (Attribute) 語法和屬性 (Property) 語法的詳細資訊,請參閱相依性屬性概觀

<!-- Simple image rendering. However, rendering an image this way may not
     result in the best use of application memory. See markup below which
     creates the same end result but using less memory. -->
<Image Width="200" 
Source="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg"/>

<Image Width="200">
  <Image.Source>
    <!-- To save significant application memory, set the DecodePixelWidth or  
     DecodePixelHeight of the BitmapImage value of the image source to the desired 
     height and width of the rendered image. If you don't do this, the application will 
     cache the image as though it were rendered as its normal size rather than just 
     the size that is displayed. -->
    <!-- Note: In order to preserve aspect ratio, only set either DecodePixelWidth
         or DecodePixelHeight but not both. -->
    <BitmapImage DecodePixelWidth="200"  
     UriSource="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg" />
  </Image.Source>
</Image>

許多範例都會使用 BitmapImage 物件來參考影像檔。 BitmapImage 是針對可延伸應用程式標記語言 (XAML) 載入優化的特製 BitmapSource 化,而且是將影像 Source 顯示為控制項的 Image 簡單方式。

下列範例示範如何使用程式碼來轉譯寬度為 200 像素的影像。

注意

BitmapImage 會實作 ISupportInitialize 介面,以優化多個屬性上的初始化。 只有在物件初始化期間,才會發生屬性變更。 呼叫 BeginInit 來表示初始化已經開始,並 EndInit 發出初始化已完成的訊號。 初始化之後,所做的屬性變更都會被忽略。

// Create Image Element
Image myImage = new Image();
myImage.Width = 200;

// Create source
BitmapImage myBitmapImage = new BitmapImage();

// BitmapImage.UriSource must be in a BeginInit/EndInit block
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg");

// To save significant application memory, set the DecodePixelWidth or
// DecodePixelHeight of the BitmapImage value of the image source to the desired
// height or width of the rendered image. If you don't do this, the application will
// cache the image as though it were rendered as its normal size rather than just
// the size that is displayed.
// Note: In order to preserve aspect ratio, set DecodePixelWidth
// or DecodePixelHeight but not both.
myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();
//set image source
myImage.Source = myBitmapImage;
' Create Image Element
Dim myImage As New Image()
myImage.Width = 200

' Create source
Dim myBitmapImage As New BitmapImage()

' BitmapImage.UriSource must be in a BeginInit/EndInit block
myBitmapImage.BeginInit()
myBitmapImage.UriSource = New Uri("C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg")

' To save significant application memory, set the DecodePixelWidth or  
' DecodePixelHeight of the BitmapImage value of the image source to the desired 
' height or width of the rendered image. If you don't do this, the application will 
' cache the image as though it were rendered as its normal size rather than just 
' the size that is displayed.
' Note: In order to preserve aspect ratio, set DecodePixelWidth
' or DecodePixelHeight but not both.
myBitmapImage.DecodePixelWidth = 200
myBitmapImage.EndInit()
'set image source
myImage.Source = myBitmapImage

旋轉、轉換和裁剪影像

WPF 可讓使用者使用 BitmapImage 的屬性,或使用 或 BitmapSourceFormatConvertedBitmap 等其他 CroppedBitmap 物件來轉換影像。 這些影像轉換作業可以縮放或旋轉影像、變更影像的像素格式,或裁剪影像。

影像旋轉是使用 RotationBitmapImage 屬性來執行。 只能以 90 度遞增的角度旋轉。 在下列範例中,影像會旋轉 90 度。

<Image Width="150" Margin="5" Grid.Column="0" Grid.Row="1">
  <Image.Source>
    <TransformedBitmap Source="/sampleImages/watermelon.jpg" >
      <TransformedBitmap.Transform>
        <RotateTransform Angle="90"/>
      </TransformedBitmap.Transform>
    </TransformedBitmap>
  </Image.Source>
</Image>
// Create Image element.
Image rotated90 = new Image();
rotated90.Width = 150;

// Create the TransformedBitmap to use as the Image source.
TransformedBitmap tb = new TransformedBitmap();

// Create the source to use as the tb source.
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(@"sampleImages/watermelon.jpg", UriKind.RelativeOrAbsolute);
bi.EndInit();

// Properties must be set between BeginInit and EndInit calls.
tb.BeginInit();
tb.Source = bi;
// Set image rotation.
RotateTransform transform = new RotateTransform(90);
tb.Transform = transform;
tb.EndInit();
// Set the Image source.
rotated90.Source = tb;
' Create Image element.
Dim rotated90 As New Image()
rotated90.Width = 150

' Create the TransformedBitmap to use as the Image source.
Dim tb As New TransformedBitmap()

' Create the source to use as the tb source.
Dim bi As New BitmapImage()
bi.BeginInit()
bi.UriSource = New Uri("sampleImages/watermelon.jpg", UriKind.RelativeOrAbsolute)
bi.EndInit()

' Properties must be set between BeginInit and EndInit calls.
tb.BeginInit()
tb.Source = bi
' Set image rotation.
Dim transform As New RotateTransform(90)
tb.Transform = transform
tb.EndInit()
' Set the Image source.
rotated90.Source = tb

使用 將 FormatConvertedBitmap 影像轉換成不同的像素格式,例如灰階。 在下列範例中,影像會 Gray4 轉換成 。

<!-- Grayscale XAML Image -->
<Image Width="200" Grid.Column="0" Grid.Row="1">
   <Image.Source>
      <FormatConvertedBitmap Source="/sampleImages/rocks.jpg"  DestinationFormat="Gray4" />
   </Image.Source>
</Image>
//Create Image Element
Image grayImage = new Image();
grayImage.Width = 200;
grayImage.Margin = new Thickness(5);

//Create source using xaml defined resource.
FormatConvertedBitmap fcb = new FormatConvertedBitmap(
   (BitmapImage)this.Resources["masterImage"],PixelFormats.Gray4,null,0);
//set image source
grayImage.Source = fcb;
'Create Image Element
Dim grayImage As New Image()
grayImage.Width = 200
grayImage.Margin = New Thickness(5)

'Create source using xaml defined resource.
Dim fcb As New FormatConvertedBitmap(CType(Me.Resources("masterImage"), BitmapImage), PixelFormats.Gray4, Nothing, 0)
'set image source
grayImage.Source = fcb

若要裁剪影像, Clip 可以使用 或 CroppedBitmapImage 屬性。 通常,如果您只想要顯示影像的一部分, Clip 則應該使用 。 如果您需要編碼並儲存裁剪的影像, CroppedBitmap 應該使用 。 在下列範例中,使用 Clip 屬性 EllipseGeometry 使用 裁剪影像。

<!-- Cropping an Image using Clip -->
<Image Width="200" Grid.Column="0" Grid.Row="5" Margin="5"
   Source="/sampleImages/gecko.jpg">
  <Image.Clip>
    <EllipseGeometry Center="75,50" RadiusX="50" RadiusY="25" />
  </Image.Clip>
</Image>
//Create the image for clipping
Image clipImage = new Image();
clipImage.Width = 200;
clipImage.Margin = new Thickness(5);

//Create & Set source
BitmapImage bi = new BitmapImage();
//BitmapImage.UriSource must be in a BeginInit/EndInit block
bi.BeginInit();
bi.UriSource = new Uri("pack://application:,,/sampleImages/gecko.jpg");
bi.EndInit();
clipImage.Source = bi;

//Clip the using an EllipseGeometry
EllipseGeometry clipGeometry = new EllipseGeometry(new Point(75, 50), 50, 25);
clipImage.Clip = clipGeometry;
' Create the image for clipping
Dim clipImage As New Image()
clipImage.Width = 200
clipImage.Margin = New Thickness(5)

'Create & Set source
Dim bi As New BitmapImage()
' BitmapImage properties must be in a BeginInit/EndInit block
bi.BeginInit()
bi.UriSource = New Uri("pack://application:,,/sampleImages/gecko.jpg")
bi.EndInit()
clipImage.Source = bi

' Clip the using an EllipseGeometry
Dim clipGeometry As New EllipseGeometry(New System.Windows.Point(75, 50), 50, 25)
clipImage.Clip = clipGeometry

縮放影像

屬性 Stretch 會控制影像如何延展以填滿其容器。 屬性 Stretch 接受列舉所 Stretch 定義的下列值:

  • None:影像不會伸展以填滿輸出區域。 如果影像大於輸出區域,會將影像繪製到輸出區域,並裁剪超過的部分。

  • Fill:影像會縮放以符合輸出區域。 因為影像的高度和寬度是分開縮放,所以可能不會維持影像的原始外觀比例。 也就是說,影像可能會變形以完全填滿輸出容器。

  • Uniform:影像會縮放,使其完全符合輸出區域。 會維持影像的外觀比例。

  • UniformToFill:縮放影像,使其完全填滿輸出區域,同時保留影像的原始外觀比例。

下列範例會將每個可用的 Stretch 列舉套用至 Image

下圖顯示範例的輸出,並示範套用至影像時,對不同 Stretch 設定的影響。

Different TileBrush Stretch settings
不同的縮放設定

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  <DockPanel>

    <Border DockPanel.Dock="Top" Background="Black">
      <TextBlock Foreground="White" HorizontalAlignment="Stretch" FontSize="20">
        Stretching an Image
      </TextBlock>
    </Border>

    <Grid Name="simpleGrid" Background="{StaticResource CheckeredBrushResource}" 
       Margin="10" 
       ShowGridLines="True"
       VerticalAlignment="Center"
       HorizontalAlignment="Center">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="175" />
        <ColumnDefinition Width="175" />
        <ColumnDefinition Width="175" />
        <ColumnDefinition Width="175" />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="200"/>
      </Grid.RowDefinitions>
      <!-- Labels -->
      <TextBlock Style="{StaticResource Header1}" 
        Grid.Column="0" Grid.Row="0">None</TextBlock>
      <TextBlock Style="{StaticResource Header1}" 
        Grid.Column="1" Grid.Row="0">Uniform</TextBlock>
      <TextBlock Style="{StaticResource Header1}" 
        Grid.Column="2" Grid.Row="0">UniformToFill</TextBlock>
      <TextBlock Style="{StaticResource Header1}"
        Grid.Column="3" Grid.Row="0">Fill</TextBlock>
      <Border Grid.Column="0" Grid.Row="1" BorderThickness="1" BorderBrush="Black">
        <!-- None: Image is not scaled. If image is larger than the
             output area, the image will be cropped to the size of the output area.-->
        <Image
          Source="sampleImages/gecko.jpg" 
          Stretch="None" />
      </Border>
      <Border Grid.Column="1" Grid.Row="1" BorderThickness="1" BorderBrush="Black">
        <!-- Uniform: Scale to fit output area.
             Aspect ratio is preserved.-->
        <Image
          Source="sampleImages/gecko.jpg" 
          Stretch="Uniform" />
      </Border>
      <Border Grid.Column="2" Grid.Row="1" BorderThickness="1" BorderBrush="Black">
        <!-- UniformToFill: Scale to completely fill output area.
             Aspect ratio is preserved. Cropping may occur.-->
        <Image  
          Source="sampleImages/gecko.jpg" 
        Stretch="UniformToFill" />
      </Border>
      <Border Grid.Column="3" Grid.Row="1" BorderThickness="1" BorderBrush="Black">
      <!-- Fill: Scale to completely fill output area.
             Aspect ratio may not be preserved.-->
      <Image 
        Source="sampleImages/gecko.jpg" 
        Stretch="Fill" />
      </Border>
    </Grid>
  </DockPanel>
</Page>

以影像繪製

您也可以使用 Brush 繪製 ,以在應用程式中顯示影像。 筆刷可讓您使用從簡單、純色到複雜圖案和影像集的任何專案來繪製 UI 物件。 若要使用影像繪製,請使用 ImageBrushImageBrush是 的 TileBrush 型別,會將其內容定義為點陣圖影像。 會顯示 ImageBrush 由其 ImageSource 屬性指定的單一影像。 您可以控制影像縮放、對齊及並排的方式,以防止扭曲並且創造出圖樣和其他效果。 下圖顯示可使用 的 ImageBrush 一些效果。

ImageBrush output examples
影像筆刷可以填滿圖形、控制項、文字等等

下列範例示範如何使用 繪製具有影像 ImageBrush 之按鈕的背景。

<!-- Sets the button's Background property with an ImageBrush. The resulting
     button has an image as its background. -->
<Button Grid.Row="3" Grid.Column="2" 
 Height="75" Width="100" Foreground="White" FontWeight="Bold"
 HorizontalAlignment="Left">
  A Button
  <Button.Background>
    <ImageBrush ImageSource="sampleImages\blueberries.jpg" />
  </Button.Background>
</Button>

如需和繪製影像的詳細資訊 ImageBrush ,請參閱 使用影像、繪圖和視覺效果 小畫家。

影像中繼資料

某些影像檔包含描述檔案內容或特性的中繼資料。 例如,大部分數位相機建立的影像,會包含用來擷取影像之相機廠牌與型號的中繼資料。 每個影像格式會以不同的方式處理中繼資料,但 WPF 映射處理會針對每個支援的影像格式提供統一的儲存和擷取中繼資料的方式。

中繼資料的存取權是透過 Metadata 物件的 屬性 BitmapSource 來提供。 Metadata 會傳 BitmapMetadata 回 物件,其中包含影像所包含的所有中繼資料。 此資料可能位於單一的中繼資料結構描述,或不同結構描述的組合中。 WPF 映射支援下列影像中繼資料架構:可交換影像檔案(Exif)、tEXt(PNG 文字資料)、影像檔目錄(IFD)、國際新聞電信理事會(IPTC)和可延伸中繼資料平臺(XMP)。

為了簡化讀取中繼資料的程式,提供數個可輕易存取的具名屬性, BitmapMetadata 例如 AuthorTitleCameraModel 。 許多具名屬性也可以用於寫入中繼資料。 讀取中繼資料的其他支援是由中繼資料查詢讀取器提供。 方法 GetQuery 可用來藉由提供字串查詢,例如 「/app1/exif/」 來擷取中繼資料查詢讀取器。 在下列範例中, GetQuery 是用來取得儲存在 「/Text/Description」 位置的 文字。


// Add the metadata of the bitmap image to the text block.
TextBlock^ myTextBlock = gcnew TextBlock();
myTextBlock->Text = "The Description metadata of this image is: " + pngInplace->GetQuery("/Text/Description")->ToString();

// Add the metadata of the bitmap image to the text block.
TextBlock myTextBlock = new TextBlock();
myTextBlock.Text = "The Description metadata of this image is: " + pngInplace.GetQuery("/Text/Description").ToString();
' Add the metadata of the bitmap image to the text block.
Dim myTextBlock As New TextBlock()
myTextBlock.Text = "The Description metadata of this image is: " + pngInplace.GetQuery("/Text/Description").ToString()

若要寫入中繼資料,必須使用中繼資料查詢寫入器。 SetQuery 會取得查詢寫入器,並設定所需的值。 在下列範例中, SetQuery 是用來寫入儲存在 「/Text/Description」 位置的 文字。

Stream^ pngStream = gcnew FileStream("smiley.png", FileMode::Open, FileAccess::ReadWrite, FileShare::ReadWrite);
PngBitmapDecoder^ pngDecoder = gcnew PngBitmapDecoder(pngStream, BitmapCreateOptions::PreservePixelFormat, BitmapCacheOption::Default);
BitmapFrame^ pngFrame = pngDecoder->Frames[0];
InPlaceBitmapMetadataWriter^ pngInplace = pngFrame->CreateInPlaceBitmapMetadataWriter();
if (pngInplace->TrySave() == true)
{
   pngInplace->SetQuery("/Text/Description", "Have a nice day.");
}
pngStream->Close();
Stream pngStream = new System.IO.FileStream("smiley.png", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
PngBitmapDecoder pngDecoder = new PngBitmapDecoder(pngStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapFrame pngFrame = pngDecoder.Frames[0];
InPlaceBitmapMetadataWriter pngInplace = pngFrame.CreateInPlaceBitmapMetadataWriter();
if (pngInplace.TrySave() == true)
{ pngInplace.SetQuery("/Text/Description", "Have a nice day."); }
pngStream.Close();
Dim pngStream As New System.IO.FileStream("smiley.png", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
Dim pngDecoder As New PngBitmapDecoder(pngStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default)
Dim pngFrame As BitmapFrame = pngDecoder.Frames(0)
Dim pngInplace As InPlaceBitmapMetadataWriter = pngFrame.CreateInPlaceBitmapMetadataWriter()
If pngInplace.TrySave() = True Then
    pngInplace.SetQuery("/Text/Description", "Have a nice day.")
End If
pngStream.Close()

轉碼器擴充性

WPF 映射處理的核心功能是新影像編解碼器的擴充性模型。 這些非受控介面可讓編解碼器開發人員整合編解碼器與 WPF,讓 WPF 應用程式自動使用新的影像格式。

如需擴充性 API 的範例,請參閱 Win32 範例編解碼器 。 此範例示範如何針對自訂影像格式建立解碼器和編碼器。

注意

轉碼器必須經過數位簽署,系統才能辨識它。

另請參閱