Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDI+: Set all pixels to given color while retaining existing alpha value

Tags:

c#

bitmap

gdi+

What is the best way to set the RGB components of every pixel in a System.Drawing.Bitmap to a single, solid color? If possible, I'd like to avoid manually looping through each pixel to do this.

Note: I want to keep the same alpha component from the original bitmap. I only want to change the RGB values.

I looked into using a ColorMatrix or ColorMap, but I couldn't find any way to set all pixels to a specific given color with either approach.

like image 413
Charles Avatar asked Mar 24 '10 17:03

Charles


1 Answers

Yes, use a ColorMatrix. It ought to look like this:

  0  0  0  0  0
  0  0  0  0  0
  0  0  0  0  0 
  0  0  0  1  0 
  R  G  B  0  1

Where R, G and B are the scaled color values of the replacement color (divide by 255.0f)

like image 74
Hans Passant Avatar answered Oct 11 '22 18:10

Hans Passant