イメージングの概要
このトピックでは、Microsoft Windows Presentation Foundation イメージング コンポーネントの概要について説明します。 WPF イメージングを使用すると、開発者はイメージの表示、変換、書式設定を行うことができます。
WPF イメージング コンポーネント
WPF イメージングでは、Microsoft Windows 内のイメージング機能が大幅に強化されています。 ビットマップの表示や共通コントロールでのイメージの使用などのイメージング機能は、以前は Microsoft Windows グラフィックス デバイス インターフェイス (GDI) または Microsoft Windows GDI+ ライブラリに依存していました。 これらの API はベースライン イメージング機能を提供しますが、コーデックの拡張性や忠実度の高い画像のサポートなどの機能がありません。 WPF イメージングは、GDI と GDI+ の欠点を克服し、アプリケーション内でイメージを表示および使用するための新しい API セットを提供するように設計されています。
WPF イメージング API にアクセスするには、マネージド コンポーネントとアンマネージド コンポーネントの 2 つの方法があります。 アンマネージド コンポーネントには、次の機能があります。
新しいイメージ形式または独自のイメージ形式の拡張モデル。
ビットマップ (BMP)、Joint Photos Experts Group (JPEG)、Portable Network Graphics (PNG)、Tagged Image File Format (TIFF)、Microsoft Windows Media Photo、Graphics Interchange Format (GIF)、アイコン (.ico) など、ネイティブイメージ形式のパフォーマンスとセキュリティが向上しました。
チャンネルあたり最大 8 ビット (ピクセルあたり 32 ビット) の高ビット深度画像データの保持。
画像を非破壊でスケーリング、トリミング、回転する。
簡略化された色管理。
ファイル内の独自のメタデータのサポート。
マネージド コンポーネントは、アンマネージド インフラストラクチャを利用して、ユーザー インターフェイス (UI)、アニメーション、グラフィックスなどの他の WPF 機能とイメージをシームレスに統合します。 また、マネージド コンポーネントは、WPF アプリケーションで新しいイメージ形式を自動的に認識できるようにする Windows Presentation Foundation (WPF) イメージング コーデック拡張モデルの利点もあります。
マネージド WPF Imaging API の大部分は System.Windows.Media.Imaging 名前空間に存在しますが、ImageBrush や ImageDrawing など、いくつかの重要な型は System.Windows.Media 名前空間に存在し、Image は System.Windows.Controls 名前空間に存在します。
このトピックでは、マネージド コンポーネントに関する追加情報を提供します。 アンマネージ API の詳細については、アンマネージド WPF イメージング コンポーネント ドキュメントを参照してください。
WPF イメージ形式
コーデックは、特定のメディア形式をデコードまたはエンコードするために使用されます。 WPF イメージングには、BMP、JPEG、PNG、TIFF、Windows Media Photo、GIF、ICON の画像形式用のコーデックが含まれています。 これらの各コーデックを使用すると、アプリケーションはデコードし、ICON を除き、それぞれのイメージ形式をエンコードできます。
BitmapSource は、画像のデコードとエンコードで使用される重要なクラスです。 これは WPF イメージング パイプラインの基本的な構成要素であり、特定のサイズと解像度で 1 つの一定のピクセル セットを表します。 BitmapSource には、複数のフレーム イメージの個々のフレームを指定することも、BitmapSourceに対して実行された変換の結果を指定することもできます。 これは、BitmapFrameなどの WPF イメージングで使用される多くのプライマリ クラスの親です。
BitmapFrame は、イメージ形式の実際のビットマップ データを格納するために使用されます。 GIF や TIFF などの形式ではイメージごとに複数のフレームがサポートされますが、多くの画像形式では 1 つの BitmapFrameのみがサポートされます。 フレームはデコーダーによって入力データとして使用され、エンコーダーに渡されて画像ファイルが作成されます。
次の例では、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 イメージング インターフェイスを使用して開発され、システムに登録されたカスタムフォーマットデコーダーは、デコーダーの選択に自動的に参加します。 これにより、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 は 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 は、拡張アプリケーション マークアップ言語 (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 のプロパティを使用するか、CroppedBitmap や FormatConvertedBitmapなどの追加の BitmapSource オブジェクトを使用して、イメージを変換できます。 これらの画像変換では、画像の拡大縮小や回転、画像のピクセル形式の変更、画像のトリミングを行うことができます。
イメージの回転は、BitmapImageの Rotation プロパティを使用して実行されます。 回転は、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
イメージをトリミングするには、Image または CroppedBitmap の Clip プロパティを使用できます。 通常、画像の一部だけを表示する場合は、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 設定に与える影響を示しています。
さまざまなストレッチ設定
<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を使用して、ボタンの背景をイメージで描画する方法を示します。
<!-- 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 オブジェクトを返します。 このデータは、1 つのメタデータ スキーマまたは異なるスキームの組み合わせに含まれる場合があります。 WPF Imaging では、Exchangeable Image File (Exif)、tEXt (PNG Textual Data)、イメージ ファイル ディレクトリ (IFD)、International Press Telecommunications Council (IPTC)、Extensible Metadata Platform (XMP) のイメージ メタデータ スキーマがサポートされています。
メタデータの読み取りプロセスを簡略化するために、BitmapMetadata には、Author、Title、CameraModelなど、簡単にアクセスできるいくつかの名前付きプロパティが用意されています。 これらの名前付きプロパティの多くは、メタデータの書き込みにも使用できます。 メタデータの読み取りの追加サポートは、メタデータ クエリ リーダーによって提供されます。 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 サンプル コーデック
手記
コーデックを認識するには、システムがデジタル署名する必要があります。
関連項目
.NET Desktop feedback