By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,115 Members | 898 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,115 IT Pros & Developers. It's quick & easy.

How do I add colorize a black & white image?

P: n/a
Let's say I have an image which is monochrome with areas of white, black and
shades of gray. Now I want to turn all the white parts into some RGB color
but it also needs to be darkened by the gray areas for the same color.

Like say it's an image of a sphere, it is white in the part it is under the
light but then as you get away from the light source it becomes darker and
darker gray. Now I want to take this image and apply just one RGB color to
it, so it becomes say a red sohere, blue one, whatever. So that parts that
were whitest and purest red, and then the red gets darker where the grays are
darker.

It's easy to do this manually in something like photoshop, but I need to
make a a windows app that does this programmatically depending on what RGB
choice user makes.

Any ideas how?
Oct 26 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
If you want to do any kind of image processing in .NET and you don't
want to wait a year for an operation to complete, you need to be
working in "unsafe" code. First take some time and look into the
"LockBits" method (I think it's in user32.dll). Using this method you
can move pixel by pixel through your image and set it's RGB values.

A good place to start when researching anything visual in Windows is
www.bobpowell.net, in your case I would focus on his GDI+ FAQ
(http://www.bobpowell.net/faqmain.htm). There you can read about
LockBits and how to use it as well as find some sample code. If you're
still having trouble after that post some of the code you're using.
Good luck!

MrNobody wrote:
Let's say I have an image which is monochrome with areas of white, black and
shades of gray. Now I want to turn all the white parts into some RGB color
but it also needs to be darkened by the gray areas for the same color.

Like say it's an image of a sphere, it is white in the part it is under the
light but then as you get away from the light source it becomes darker and
darker gray. Now I want to take this image and apply just one RGB color to
it, so it becomes say a red sohere, blue one, whatever. So that parts that
were whitest and purest red, and then the red gets darker where the grays are
darker.

It's easy to do this manually in something like photoshop, but I need to
make a a windows app that does this programmatically depending on what RGB
choice user makes.

Any ideas how?
Oct 26 '06 #2

P: n/a
Thanks justin that is a very helpful link.

Would you happen to know where I can go to form some basic color type
questions?

Like what exactly would I have to do to give each pixel a red color but
darkened/lightened depending on how gray it is? Wouldn't I have to change the
RGB value given depending on the shade of gray the pixel is, so the end
result is not just pure saturated color?
Oct 26 '06 #3

P: n/a

MrNobody wrote:
Thanks justin that is a very helpful link.

Would you happen to know where I can go to form some basic color type
questions?

Like what exactly would I have to do to give each pixel a red color but
darkened/lightened depending on how gray it is? Wouldn't I have to change the
RGB value given depending on the shade of gray the pixel is, so the end
result is not just pure saturated color?
Exactly right.

If your destination hue was red, green, or blue, it would be easy: just
keep the R, G, or B value for the color you want, and set the other two
to zero. But it probably won't be red, green, or blue that you want.
You'll have to start with an HSV color value, adjusting the color
percentage that you want, and convert back to RGB.

You can learn a lot about color on wikipedia.
http://en.wikipedia.org/wiki/HSV_color_space

There are commandline utilities, and commercial libraries, that do the
work for you. Try out graphicsmagick: http://www.graphicsmagick.org

As an example, with graphicsmagic, converting from grayscale to
shades-of-blue would be
gm convert input.png -colorize 255/255/0 output.png

Oct 26 '06 #4

P: n/a
If all you are trying to do is apply a red, blue, or green hue that
matches how much grey is in the image that is not too difficult. First
you need to determine what format your gray image is in. This is
because different image formats have different sizes. The BitmapData
class will have the necessary information here. Once you have a
numerical value for how much gray there is you can set the correct
color to that amount. To do that you do the following.

- create an empty Bitmap the same size as your gray one.
- iterate through your gray image using your BitmapData object and get
gray value.
- create a pixel in your empty Bitmap with the same amount of whatever
color you want to set as a hue. The byte you add the value to for the
color is reached with your BitmapData object, but how you find the
right byte depends on the image format you use.
- when you're done save your new Bitmap.

Now, this will only work to add one hue to the image, so you'll go from
a gray image to a red image, or a blue image, etc. Hope that helps,
good luck.

MrNobody wrote:
Thanks justin that is a very helpful link.

Would you happen to know where I can go to form some basic color type
questions?

Like what exactly would I have to do to give each pixel a red color but
darkened/lightened depending on how gray it is? Wouldn't I have to change the
RGB value given depending on the shade of gray the pixel is, so the end
result is not just pure saturated color?
Oct 27 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.