イメージングの概要

このトピックでは、Microsoft Windows Presentation Foundation Imaging コンポーネントの概要を示します。 開発者は、WPF Imaging を使用して、イメージの表示、変換、および形式設定を実行できます。

WPF Imaging Component

WPF Imaging は、Microsoft Windows 内のイメージング機能を大幅に強化します。 ビットマップの表示や一般的なコントロールでのイメージの使用などのイメージング機能は、これまでは Microsoft Windows Graphics Device Interface (GDI) ライブラリまたは Microsoft Windows GDI+ ライブラリに頼っていました。 これらの API は、基本的なイメージング機能を提供しますが、コーデックの拡張機能や高品質なイメージのサポートなどの機能が不足しています。 WPF Imaging は、GDI および GDI+ の欠点を克服し、アプリケーション内でイメージを表示および使用するための新しい API のセットを提供するために再設計されています。

WPF Imaging API にアクセスする方法は 2 つあります (マネージド コンポーネントとアンマネージド コンポーネント)。 アンマネージ コンポーネントは、次の機能を提供します。

  • 新規または独自のイメージ形式の機能拡張モデル。

  • ビットマップ (BMP)、Joint Photographics Experts Group (JPEG)、ポータブル ネットワーク グラフィックス (PNG)、Tagged Image File Format (TIFF)、Microsoft Windows Media Photo、グラフィックス インターチェンジ形式 (GIF)、アイコン (.ico) などのネイティブ イメージ形式のパフォーマンスとセキュリティの向上。

  • チャネルあたり最大 8 ビットのビット深度の高いイメージ データの保存 (ピクセルあたり 32 ビット)。

  • 非破壊的なイメージのスケーリング、クロップ、および回転。

  • 単純化されたカラー管理。

  • ファイル内での専用メタデータのサポート。

  • マネージド コンポーネントは、アンマネージド インフラストラクチャを利用して、ユーザー インターフェイス (UI)、アニメーション、グラフィックスなどの他の WPF 機能とイメージをシームレスに統合します。 マネージド コンポーネントには、Windows Presentation Foundation (WPF) のイメージング コーデック機能拡張モデルの利点もあるため、WPF アプリケーションで新しいイメージ形式を自動的に認識できます。

マネージド WPF Imaging API の大部分は System.Windows.Media.Imaging 名前空間に存在しますが、ImageBrushImageDrawing などのいくつかの重要な型は System.Windows.Media 名前空間に存在し、ImageSystem.Windows.Controls 名前空間に存在します。

このトピックでは、マネージド コンポーネントに関する追加情報について説明します。 アンマネージ API の詳細については、アンマネージ WPF Imaging コンポーネントに関するドキュメントを参照してください。

WPF イメージ形式

コーデックは、特定のメディア形式をデコードまたはエンコードするために使用されます。 WPF Imaging には、BMP、JPEG、PNG、TIFF、Windows Media Photo、GIF、および ICON イメージ形式のコーデックが含まれます。 アプリケーションは、これらのコーデックを使用して、対応するイメージ形式をデコードでき、ICON を除くイメージ形式をエンコードできます。

BitmapSource は、イメージのデコードとエンコードで使用される重要なクラスです。 これは、WPF Imaging パイプラインの基本ビルディング ブロックであり、ピクセルの単一の定数セットを特定のサイズと解像度で表します。 BitmapSource は、複数のフレーム イメージの個別のフレームでも、BitmapSource に対して実行された変換の結果でもかまいません。 これは、BitmapFrame など、WPF Imaging で使用される多くの主要クラスの親です。

BitmapFrame は、イメージ形式の実際のビットマップ データを格納するために使用されます。 多くのイメージ形式は、単一の BitmapFrame のみをサポートしますが、GIF や TIFF などの形式では、イメージごとに複数のフレームがサポートされます。 フレームは、デコーダーによって入力データとして使用され、イメージ ファイルを作成するためにエンコーダーに渡されます。

次の例は、BitmapSource から BitmapFrame を作成した後、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 でのイメージの表示」セクションの例を参照してください。 アンマネージ 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 Imaging には、上記で説明したイメージ形式用のエンコーダーが用意されています。

次の例は、エンコーダーを使用して新しく作成されたビットマップ イメージを保存する操作を示しています。

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 は、属性構文とプロパティ構文という 2 つの方法で使用できます。 次の例は、属性構文とプロパティ タグ構文の両方を使用して、幅 200 ピクセルのイメージをレンダリングする方法を示しています。 属性構文とプロパティ構文の詳細については、「依存関係プロパティの概要」を参照してください。

<!-- 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 は、Extensible Application Markup Language (XAML) の読み込み用に最適化された特殊な BitmapSource であり、イメージを Image コントロールの Source として表示するための簡単な方法です。

次の例は、コードを使用して幅 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 のプロパティを使用するか、CroppedBitmapFormatConvertedBitmap などの追加 BitmapSource オブジェクトを使用して、イメージを変換できます。 これらのイメージの変換では、イメージの拡大縮小、回転、ピクセル形式の変更、またはクロップを行うことができます。

イメージの回転は、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>
// 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

イメージをクロップするために、ImageClip プロパティまたは CroppedBitmap を使用できます。 通常、イメージの一部を表示する場合は、Clip を使用します。 クロップしたイメージをエンコードして保存する必要がある場合は、CroppedBitmap を使用します。 次の例では、EllipseGeometry を使用する Clip プロパティを使用してイメージがクロップされます。

<!-- 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
異なる Stretch 設定

<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 オブジェクトを単色で塗りつぶすことも、パターンとイメージの複雑な組み合わせで塗りつぶすこともできます。 イメージを使用して塗りつぶすには、ImageBrush を使用します。 ImageBrush は、その内容をビットマップ イメージとして定義する TileBrush の一種です。 ImageBrush は、ImageSource プロパティによって指定された 1 つのイメージを表示します。 イメージがゆがまないようしたり、パターンやその他の効果を作成したりするために、イメージの伸縮、配置、およびタイル表示方法を制御できます。 次の図は、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 Imaging では、サポートしているイメージ形式のメタデータの格納と取得を一律に実行します。

メタデータへのアクセスは、BitmapSource オブジェクトの Metadata プロパティを通して提供されます。 Metadata は、イメージによって格納されているすべてのメタデータが含まれている BitmapMetadata オブジェクトを返します。 このデータは、1 つのメタデータ スキーマでも、異なるスキーマの組み合わせでもかまいません。 WPF Imaging は、次のイメージ メタデータ スキーマをサポートしています。Exchangeable image file (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.
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 Imaging の核心的な機能は、新しいイメージ コーデック用の機能拡張モデルです。 これらのアンマネージ インターフェイスによって、コーデック開発者は、コーデックを WPF と統合して、新しいイメージ形式を WPF アプリケーションで自動的に使用できるようにすることができます。

機能拡張 API のサンプルについては、Win32 サンプル コーデックに関するページを参照してください。 このサンプルは、カスタム イメージ形式用のデコーダーとエンコーダーの作成方法を示しています。

注意

システムで認識するは、コーデックをデジタル署名する必要があります。

関連項目