リマップとは、イメージ内の色をカラー リマップ テーブルに基づいて変換するプロセスのことです。 カラー リマップ テーブルは、ColorMap オブジェクトの配列です。 配列内の各 ColorMap オブジェクトは OldColor プロパティと NewColor プロパティを持ちます。
GDI+ がイメージを描画する場合、そのイメージの各ピクセルが古い色の配列と比較されます。 ピクセルの色が古い色と一致すると、その色は対応する新しい色に変更されます。 色が変更されるのは描画時だけであり、Image オブジェクトまたは Bitmap オブジェクトに格納されているイメージ自体のカラー値は変更されません。
リマップされたイメージを描画するには、ColorMap オブジェクトの配列を初期化します。 この配列を ImageAttributes オブジェクトの SetRemapTable メソッドに渡し、その ImageAttributes オブジェクトを Graphics オブジェクトの DrawImage メソッドに渡します。
使用例
ファイル RemapInput.bmp から Image オブジェクトを作成する例を次に示します。 このコードは、1 つの ColorMap オブジェクトから成るカラー リマップ テーブルを作成します。 ColorRemap オブジェクトの OldColor プロパティは赤で、NewColor プロパティは青です。 リマップを適用しない場合と適用する場合それぞれに 1 回ずつイメージが描画されます。 このリマップ プロセスでは、すべての赤いピクセルが青に変更されます。
次の図は、左側に元のイメージ、右側にリマップされたイメージを示しています。
Dim image As New Bitmap("RemapInput.bmp")
Dim imageAttributes As New ImageAttributes()
Dim width As Integer = image.Width
Dim height As Integer = image.Height
Dim colorMap As New ColorMap()
colorMap.OldColor = Color.FromArgb(255, 255, 0, 0) ' opaque red
colorMap.NewColor = Color.FromArgb(255, 0, 0, 255) ' opaque blue
Dim remapTable As ColorMap() = {colorMap}
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap)
e.Graphics.DrawImage(image, 10, 10, width, height)
' Pass in the destination rectangle (2nd argument), the upper-left corner
' (3rd and 4th arguments), width (5th argument), and height (6th
' argument) of the source rectangle.
e.Graphics.DrawImage( _
image, _
New Rectangle(150, 10, width, height), _
0, 0, _
width, _
height, _
GraphicsUnit.Pixel, _
imageAttributes)
Image image = new Bitmap("RemapInput.bmp");
ImageAttributes imageAttributes = new ImageAttributes();
int width = image.Width;
int height = image.Height;
ColorMap colorMap = new ColorMap();
colorMap.OldColor = Color.FromArgb(255, 255, 0, 0); // opaque red
colorMap.NewColor = Color.FromArgb(255, 0, 0, 255); // opaque blue
ColorMap[] remapTable = { colorMap };
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
e.Graphics.DrawImage(image, 10, 10, width, height);
e.Graphics.DrawImage(
image,
new Rectangle(150, 10, width, height), // destination rectangle
0, 0, // upper-left corner of source rectangle
width, // width of source rectangle
height, // height of source rectangle
GraphicsUnit.Pixel,
imageAttributes);
コードのコンパイル
前述の例は Windows フォームと一緒に使用することが想定されていて、Paint イベント ハンドラーのパラメーターである PaintEventArgs e が必要です。