Share via


影像處理概觀

更新:2007 年 11 月

本主題提供 Microsoft Windows Presentation Foundation 影像處理元件 的簡介。WPF 影像處理讓開發人員可以顯示、轉換及格式化影像。

這個主題包含下列章節。

  • WPF 影像處理元件
  • WPF 影像格式
  • 在 WPF 中顯示影像
  • 影像中繼資料
  • 轉碼器擴充性
  • 相關主題

WPF 影像處理元件

WPF 影像處理提供 Microsoft Windows 內重要的影像處理增強功能。之前的影像處理功能 (例如顯示點陣圖或在通用控制項上使用影像) 要依賴 Microsoft Windows 繪圖裝置介面 (GDI) 或 Microsoft Windows GDI+ 程式庫運作。這些 API 提供基準的影像處理功能,但是缺少一些功能,例如支援轉碼器擴充性和高精確度影像支援。WPF 影像處理的設計可克服 GDI 和 GDI+ 的缺點,並提供一組新的 API 以在應用程式中顯示及使用影像。

有兩種方法可以存取 WPF 影像處理 API,也就是 Managed 元件和 Unmanaged 元件。Unmanaged 元件會提供下列功能。

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

  • 點陣圖 (BMP)、Joint Photographic Experts Group (JPEG)、Portable Network Graphics (PNG)、標記的影像檔案格式 (TIFF)、Microsoft Windows Media Photo、圖形交換格式 (GIF) 及圖示 (.ico) 等原生影像格式的改善效能及安全性。

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

  • 非解構性的影像縮放、裁剪及旋轉。

  • 簡化的色彩管理。

  • 支援檔案中、專屬中繼資料 (Metadata)。

  • Managed 元件會利用 Unmanaged 基礎結構提供影像與其他 WPF 功能 (例如使用者介面 (UI)、動畫及圖形) 的緊密整合。Managed 元件還受益於 Windows Presentation Foundation (WPF) 影像處理轉碼器擴充性模型,該模型會自動辨識 WPF 應用程式中的新影像格式。

大多數 Managed 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 Media Photo、GIF 和 ICON 影像格式適用的轉碼器。每個轉碼器都可以讓應用程式解碼和編碼各自的影像格式 (但 ICON 在編碼部分是例外)。

BitmapSource 是重要的類別,用在影像的解碼和編碼。它是 WPF 影像處理管線的基本建置組塊,代表在特定大小和解析度下的單一固定像素集。BitmapSource 可以是多畫面格影像中的個別畫面格,或者是對 BitmapSource 執行轉換的結果。它是許多 WPF 影像處理 (例如 BitmapFrame) 中使用之主要類別的父代 (Parent)。

BitmapFrame 可以用來儲存影像格式的實際點陣圖資料。許多影像格式只支援單一 BitmapFrame,而例如 GIF 和 TIFF 的格式則支援每個影像有多畫面格。畫面格會由解碼器做為輸入資料並傳送至編碼器以建立影像檔。

下列範例示範 BitmapFrame 如何從 BitmapSource 建立,然後加入至 TIFF 影像。

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

影像格式解碼

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

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

' 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)
// 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);

影像格式編碼

影像編碼是指將影像資料變成特定影像格式的轉譯作業。然後就可以使用編碼後的影像資料建立新的影像檔。WPF 影像處理提供上述每個影像格式的編碼器。

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

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

在 WPF 中顯示影像

有幾種方法可以在 Windows Presentation Foundation (WPF) 應用程式中顯示影像。顯示影像的方法有使用 Image 控制項顯示影像,使用 ImageBrush 在視覺物件上繪製影像,或使用 ImageDrawing 繪製影像。

使用影像控制項

Image 是一項架構項目,也是在應用程式中顯示影像的主要方法。在 XAML 中,有兩種方法可以使用 Image,也就是屬性 (Attribute) 語法和屬性 (Property) 語法。下列範例顯示如何使用屬性 (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 then 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 是一種特製化的 BitmapSource,專門針對可延伸標記語言 (XAML) 載入進行最佳化,並且是將影像顯示為 Image 控制項的 Source 的一個簡單方法。

下列範例顯示如何使用程式碼呈現 200 像素寬的影像。

注意事項:

BitmapImage 會實作 ISupportInitialize 介面以最佳化多個屬性的初始化作業。屬性變更只能在物件初始化期間進行。呼叫 BeginInit 表示初始化已經開始,呼叫 EndInit 則表示初始化已經完成。初始化之後所做的屬性變更會被忽略。

' 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 then 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
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 then 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 的屬性,以及使用其他 BitmapSource 物件,例如 CroppedBitmapFormatConvertedBitmap。這些影像轉換作業可以縮放或旋轉影像、變更影像的像素格式,或裁剪影像。

影像旋轉作業是使用 BitmapImageRotation 屬性執行。只能以 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>
<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.
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
// 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;

將影像轉換為不同的像素格式,例如使用 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
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
//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;

若要裁剪影像,可以使用 ImageCroppedBitmapClip 屬性。通常,如果您只要顯示影像的某一部分,應該使用 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
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
//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;

自動縮放影像

Stretch 屬性控制影像如何自動縮放以填滿容器。Stretch 屬性接受下列值 (由 Stretch 列舉型別定義):

  • None:影像不會自動縮放以填滿輸出區域。如果影像大於輸出區域,會將影像繪製至輸出區域,並裁剪超過的部分。

  • Fill:影像會隨縮放以符合輸出區域的大小。因為影像的高度和寬度是分開縮放的,所以可能不會保留影像的原始外觀比例 (Aspect Ratio)。也就是說,影像可能會扭曲以完全填滿輸出容器。

  • Uniform:影像會縮放以完全符合輸出區域的大小。會保留影像的外觀比例。

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

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

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

不同自動縮放設定

不同的 TileBrush Stretch 設定

<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://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 輸出範例

下列範例示範如何使用 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 影像處理可以替所有支援的影像格式提供統一的中繼資料儲存及擷取方法。

對中繼資料的存取是透過 BitmapSource 物件的 Metadata 屬性進行。Metadata 會傳回 BitmapMetadata 物件,其中包含影像內的所有中繼資料。這些資料可能位於一個中繼資料結構描述中,或位於不同結構描述中。WPF 影像處理支援下列影像中繼資料結構描述:可交換影像檔 (EXIF)、tEXt (PNG 文字資料)、影像檔目錄 (IFD)、國際電信聯盟 (IPTC) 和 Extensible Metadata Platform (XMP)。

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

' 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()
// 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();

若要寫入中繼資料,請使用中繼資料查詢寫入器。SetQuery 會取得查詢寫入器並設定想要的值。在下列範例中,SetQuery 可以寫入儲存在 "/Text/Description" 位置中的文字。

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

轉碼器擴充性

WPF 影像處理的核心功能是新影像轉碼器的擴充性模型。這些 Unmanaged 介面可以讓轉碼器開發人員整合轉碼器與 WPF,使得 WPF 應用程式可以自動使用新的影像格式。若要進一步了解 Unmanaged WPF 影像處理,請參閱 Unmanaged WPF 影像處理元件文件。

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

注意事項:

轉碼器必須有數位簽章,系統才能加以辨識。

請參閱

工作

Win32 範例轉碼器

相片存放區示範

概念

最佳化效能:2D 圖形和影像處理

參考

Unmanaged WPF 影像處理元件

BitmapSource

BitmapImage

Image

BitmapMetadata