I need to convert a bitmap from color to black and white

I am loading an image from a bitmap file into a picture box. I would like to convert the image from color to black and white.

I need to perform the conversion on the picturebox picture after the bitmap has been loaded.

This is my current approach, which is not working.

    'Paint pixels black if they are not white
    Dim Cnt1 As Byte
    Dim Cnt2 As Byte
    Dim Point As POINTAPI

    For Cnt1 = 0 To 7 'Step 5
        For Cnt2 = 0 To 10 ' Step 5
            'Draw a pixel
            If Not GetPixel(Picture1.hdc, Cnt1, Cnt2) = vbWhite Then
                SetPixel Picture1.hdc, Cnt1, Cnt2, vbBlack
            End If
        Next Cnt2
    Next Cnt1

Any ideas or other approaches would be appreciated.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DovbermanAuthor Commented:
I will try it.  I hope to modify it to convert any pixel that is not white.  This would save time.

It seems complex.  It will take a while to implement it.


DovbermanAuthor Commented:
I tried it and it works.  The color bitmap that was loaded into the picture box is transformed to grayscale.

I would like to transform it to Black and Whitel

TransformImage calls SetBitmapPixels which converts each pixel to grayscale.

'Change pixel color
          For Y = 0 To hgt - 1
                For X = 0 To wid - 1
                    With pixels(X, Y)
                        .rgbBlue = bytes(X * 4, Y)
                        .rgbGreen = bytes(X * 4 + 1, Y)
                        .rgbRed = bytes(X * 4 + 2, Y)
                    End With
                Next X
         Next Y

How can I test the pixels to determine which are not vbWhite and change them to vbBlack ?


With pixels(X, Y)
           'If pixel is not white Then change to black
                        .rgbBlue = bytes(X * 4, Y) ?
                        .rgbGreen = bytes(X * 4 + 1, Y) ?
                        .rgbRed = bytes(X * 4 + 2, Y) ?
          'End If
End With


DovbermanAuthor Commented:
Ignore the request to transform to black and white.  Grayscale is best for my application.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.