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

前端 未结 3 1757
萌比男神i
萌比男神i 2021-01-05 12:15

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 loop

3条回答
  •  暖寄归人
    2021-01-05 12:57

    The best (in terms of perf, at least) option is to use Bitmap.LockBits, and loop through the pixel data in the scan line, setting the RGB values.

    Since you don't want to change the Alpha, you are going to have to loop through each pixel - there is no single memory assignment that will preserve alpha and replace RGB, since they're interleaved together.

提交回复
热议问题