Vorgehensweise: Verwenden einer Farbneuzuordnungstabelle

Die Neuzuordnung ist der Prozess des Konvertierens der Farben in einem Bild entsprechend einer Farbneuzuordnungstabelle. Die Farbneuzuordnungstabelle ist ein Array von ColorMap-Objekten. Jedes ColorMap-Objekt im Array besitzt eine OldColor-Eigenschaft und eine NewColor-Eigenschaft.

Wenn GDI+ ein Bild zeichnet, wird jedes Pixel des Bilds mit dem Array alter Farben verglichen. Entspricht die Farbe eines Pixels einer alten Farbe, wird seine Farbe in die entsprechende neue Farbe geändert. Die Farben werden nur für das Rendern geändert – die Farbwerte des Bilds selbst (gespeichert in einem Image- oder Bitmap- Objekt) werden nicht geändert.

Zum Zeichnen eines neu zugeordneten Bilds initialisieren Sie ein Array von ColorMap-Objekten. Übergeben Sie dieses Array an die SetRemapTable-Methode eines ImageAttributes-Objekts, und übergeben Sie dann das ImageAttributes-Objekt an die DrawImage-Methode eines Graphics-Objekts.

Beispiel

Im folgenden Beispiel wird ein Image-Objekt aus der Datei „RemapInput.bmp“ erstellt. Der Code erstellt eine Farbneuzuordnungstabelle, die aus einem einzelnen ColorMap-Objekt besteht. Die OldColor-Eigenschaft des ColorRemap-Objekts ist „Rot“ (red), und die NewColor-Eigenschaft ist „Blau“ (blue). Das Bild wird einmal ohne Neuzuordnung und einmal mit gezeichnet. Der Neuzuordnungsvorgang ändert alle roten Pixel in Blau.

In der folgenden Abbildung wird links das Originalbild und rechts das neu zugeordnete Bild gezeigt.

Screenshot showing the original image and the remapped image.

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

Kompilieren des Codes

Das obige Beispiel ist für die Verwendung in Windows Forms konzipiert und erfordert PaintEventArgse, einen Parameter des Paint-Ereignishandlers.

Siehe auch