“着色” .NET 中的位图
如果您有一个包含灰度图像的 System.Drawing.Bitmap 实例,是否有内置方法可以利用另一种颜色的影响对其进行“着色”?
例如,如果您有一张咖啡杯的黑白(灰度)图片,并且您想以编程方式创建红色、绿色和紫色版本的单独图像。
If you have a System.Drawing.Bitmap
instance that contains a greyscale image, is there a built in way to "colourize" it with the influence of another colour?
For example, if you had a black and white (greyscale) picture of a coffee mug and you wanted to create separate images of red, green and purple versions programmatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果是 8 位图像,则可以使用不同的调色板 (Image.Palette)。这本质上是一个查找表,它将颜色值分配给每个可能的像素字节值。比循环更改所有像素要快得多。
If it's an 8 bit image, you can just use a different the palette (Image.Palette). That's essentially a lookup table that assigns a Color value to each possible pixel byte value. Much faster than changing all pixels in a loop.
请参阅此处
我过去曾使用过此方法。您想专门查看 ToSepia。
您可能需要稍微解构它,但它对我有用。
See here
I have used this in the past. You are wanting to look specifically at ToSepia.
You may need to deconstruct this a bit but it has worked for me.
我会创建原始图像的副本,然后他们放置一个单独的顶部所需颜色的半透明图像。
更新:请参阅 http://www.codeproject 处的示例。 com/KB/cs/Merge_Images_in_C_.aspx
I'd create a copy of the original image and them put a separate semi transparent image of the desired color of the top.
Update: see example at http://www.codeproject.com/KB/cs/Merge_Images_in_C_.aspx
我不确定内置的方式,但是,如果您将每种颜色表示为浮点而不是字节(255 变为 1 - 全强度),则将每个通道与您想要的颜色相乘应该会产生您正在讨论的效果。
不过,您确实需要对每个像素应用此功能。
I am unsure of a built in way but, if you represent each colour as a float rather than a byte (255 becomes 1 - full intensity), multiplying the each channel with your desired colour should give the effect you are talking about.
You do need to apply this per pixel though.
我没有提供代码示例,但这里有一种方法可以做到这一点。将每个像素从 RGB 转换为 HSV,并更改每个像素上的色相和饱和度分量。色调控制颜色。值应该保持不变。结果将是具有相同亮度和暗度但颜色不同的位图。
编辑:这是一个例子。注意色相和饱和度的更新。
I don't have a code example to give but here's a way to do this. Convert each pixel from RGB to HSV and change the Hue and Saturation component on each pixel. The Hue controls the Color. The Value should stay the same. The result will be a Bitmap with the same lightness and darkness but with a different color.
Edit: here's an example. Notice the Hue and Saturation update.