Freigeben über


Verwenden einer Farbumwandlungstabelle

Bei der Farbumwandlung werden die Farben in einem Bild entsprechend einer Farbumwandlungstabelle umgewandelt. Die Farbumwandlungstabelle besteht aus einem Array von ColorMap-Objekten. Jedes ColorMap-Objekt im Array verfügt über eine OldColor-Eigenschaft und eine NewColor-Eigenschaft.

Wenn ein Bild durch GDI+ gezeichnet wird, wird jedes Pixel im Bild mit dem Array der alten Farben verglichen. Falls die Farbe eines Pixels mit einer alten Farbe übereinstimmt, wird sie in die entsprechende neue Farbe geändert. Die Farben werden nur zu Renderingzwecken geändert; die Farbwerte des Bildes selbst (in einem der Objekte Image oder Bitmap gespeichert) bleiben unverändert.

Um ein neu zugeordnetes Bild zu zeichnen, initialisieren Sie ein Array von ColorMap-Objekten. Übergeben Sie erst das Array an die SetRemapTable-Methode eines ImageAttributes-Objekts und dann das ImageAttributes-Objekt an die DrawImage-Methode eines Graphics-Objekts.

Im folgenden Beispiel wird ein Image-Objekt aus der Datei RemapInput.bmp erstellt. Durch den Code wird eine Farbumwandlungstabelle erstellt, die ein einziges ColorMap-Objekt umfasst. Die OldColor-Eigenschaft des ColorRemap-Objekts entspricht Rot, und die NewColor-Eigenschaft entspricht Blau. Das Bild wird einmal ohne und einmal mit Farbumwandlung gezeichnet. Durch die Farbumwandlung werden alle roten Pixel in blaue Pixel geändert.

Dim image = 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)
[C#]
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);

In der folgenden Abbildung ist das ursprüngliche Bild auf der linken und das neu zugeordnete Bild auf der rechten Seite zu sehen.