brightness adjusting on black & white images vb.net

M

marfi95

I'm trying to implement some code in vb.net to allow the user to
adjust the brightness or contrast on an image (through the use of a
slider) that is already black & white in the bitmap. I have tried
to use the colormatrix and even down to the pixel using GetPixel and
SetPixel in system.drawing, but I'm really not up on using gdi+ and
haven't really gotten anywhere. I've seen some examples for VB6, but
I need vb.net.

I know that all the pixels are either 255 or 0 already since its black
and white, but if you are adusting the brightness, how do you decide
which pixels to turn white or black based on how much the user wants
to brighten/darken. I've got code to read & set each pixel, but I
just dont know what to use to decide what to set the new pixel to.

If you someone could post some code or point me to a good example that
explains whats going on, I would be most appreciative.

Thanks,
Mark
 
C

C-Services Holland b.v.

I'm trying to implement some code in vb.net to allow the user to
adjust the brightness or contrast on an image (through the use of a
slider) that is already black & white in the bitmap. I have tried
to use the colormatrix and even down to the pixel using GetPixel and
SetPixel in system.drawing, but I'm really not up on using gdi+ and
haven't really gotten anywhere. I've seen some examples for VB6, but
I need vb.net.

I know that all the pixels are either 255 or 0 already since its black
and white, but if you are adusting the brightness, how do you decide
which pixels to turn white or black based on how much the user wants
to brighten/darken. I've got code to read & set each pixel, but I
just dont know what to use to decide what to set the new pixel to.

If you someone could post some code or point me to a good example that
explains whats going on, I would be most appreciative.

Thanks,
Mark

If your values are at 0 or 255 you've got maximum contrast. You can't
decrease contrast for just a few pixels since there would be no way to
determine what pixels should remain white/black and what pixels should
turn some shade of grey. You could only decrease the contrast by
bringing the 0 and 255 values closer together, but don't expect some
gradient to appear.
 
M

marfi95

So you can use the ColorMatrix to adjust images even if they are
already black and white ?
 
B

Bob Powell [MVP]

Of course. The matrix doesn't care what values the RGB elements are or if
they're the same, which is all a grayscale is, equal R, G and B.

You can still change the brightness and contrast using the matrix. You can't
put saturation back in though. That's like trying to put the air back in a
burst balloon.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
M

marfi95

Thanks Bob.

I took your example of adjusting the contrast (the one with
ImageContrast as the namespace) and ran against one of my test images.
The problem was the image was able to get darker, but never got any
lighter (even when moving the trackbar all the way over). Let me
explain what I'm doing. The images we display are coming out of a
check scanner for our customers. We allow them to rescan them at
lighter/darker contrast values, but that forces them to go through the
scanner again which is not a real fast device. So what I wanted to do
is let them adjust the original image directly on the screen through
the use of a trackbar and not require them to go through the scanner
again.

The images are coming out of the scanner in a 1bpp indexed pixel format
- black/white ?

Any ideas why it wouldn't get any darker. Is there a better way to do
it.

Many thanks.
 
B

Bob Powell [MVP]

I guess the reason is because you're altering the original image, not a copy
of it.

You need to copy the original to the new setting every time, don't change an
image and then try to adjust *its* contrast / brightness later. It's a
classic degradation problem.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
M

marfi95

That makes sense. Yea, I am applying the new user settings to the
"already adjusted" image, when I should be going back to the original
each time.

I'll try it when I get in the office. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top