GDI+:将所有像素设置为给定颜色,同时保留现有的 alpha 值
将 System.Drawing.Bitmap 中每个像素的 RGB 分量设置为单一纯色的最佳方法是什么?如果可能的话,我想避免手动循环每个像素来执行此操作。
注意:我想保留原始位图中相同的 alpha 分量。我只想更改 RGB 值。
我研究过使用 ColorMatrix
或 ColorMap
,但我找不到任何方法可以使用这两种方法将所有像素设置为特定的给定颜色。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,使用 ColorMatrix。它应该看起来像这样:
其中 R、G 和 B 是替换颜色的缩放颜色值(除以 255.0f)
Yes, use a ColorMatrix. It ought to look like this:
Where R, G and B are the scaled color values of the replacement color (divide by 255.0f)
我知道这个问题已经得到解答,但根据 Hans Passant 的回答,生成的代码如下所示:
在此处下载工作演示:http://benpowell.org/change-the-color-of-a-transparent- png-图像-图标-on-the-fly-using-asp-net-mvc/
I know this is already answered, but based on Hans Passant's answer the resulting code looks something like this:
Download a working demo here: http://benpowell.org/change-the-color-of-a-transparent-png-image-icon-on-the-fly-using-asp-net-mvc/
最好的(至少就性能而言)选项是使用 Bitmap.LockBits ,并循环扫描线中的像素数据,设置RGB值。
由于您不想更改 Alpha,因此必须循环遍历每个像素 - 没有单个内存分配可以保留 Alpha 并替换 RGB,因为它们是交错在一起的。
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.