Results 1 to 9 of 9

Thread: Grayscaling images

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    Unhappy

    I can't find anything on how to make images black and white! Does anyone here know?

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Well I'm sure there's a 1000 ways to do this better but here goes.
    Code:
    Public Function GetGrayScale(lngColor As Long) As Long
        Dim nRed&, nGreen&, nBlue&
        nRed = lngColor Mod 256
        nBlue = lngColor \ &H10000
        nGreen = (lngColor Mod &H10000) \ 256
        nRed = (nRed + nBlue + nGreen) \ 3
        GetGrayScale = nRed * &H10000 + nRed * &H100 + nRed
    End Function
    You can loop through your picture and get every pixels color using the GetPixel API. Pass it to GetGrayScale and use the return value as the argument for SetPixel.

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Hmmm... That didn't come out right.
    Change
    Code:
    &H10000
    'and
    &H100
    To

    &H10000
    and
    &H100

    Whitout the spaces.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    Um.......I got lots of errors, and how would I do the whole picture?

  5. #5
    Guest
    this turns anything that is not white, black

    Code:
    Private Sub Command1_Click()
        Dim TColor As Long
        For i = 0 To Picture1.Width
            For j = 0 To Picture1.Height
                TColor = GetPixel(Picture1.hdc, i, j)
                If TColor <> vbWhite Then
                    Call SetPixel(Picture1.hdc, i, j, vbBlack)
                End If
            Next
        Next
    End Sub

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    The errors must be because the post doesn't come out right.
    What looks like this:
    &H10000 is actually hex for the value 65536
    and &H100 is hex for 256

    You can use the loop denniswrenn wrote but substitute vbBlack for GetGrayScale(TColor)
    Code:
    Call SetPixel(Picture1.hdc, i, j, GetGrayScale(TColor))
    You have to declare SetPixel and GetPixel.
    Code:
    Public Declare Function GetPixel _
     Lib "gdi32" Alias "GetPixel" ( _
     ByVal hdc As Long, _
     ByVal x As Long, _
     ByVal y As Long) As Long
    
    Public Declare Function SetPixel _
     Lib "gdi32" Alias "SetPixel" ( _
     ByVal hdc As Long, _
     ByVal x As Long, _
     ByVal y As Long, _
     ByVal crColor As Long) As Long
    Good luck!



  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    Originally posted by Joacim Andersson
    You can use the loop denniswrenn wrote but substitute vbBlack for GetGrayScale(TColor)
    Code:
    Call SetPixel(Picture1.hdc, i, j, GetGrayScale(TColor))
    I get an error: sub or function not defined
    Code:
        Dim TColor As Long
        For i = 0 To Picture1.Width
            For j = 0 To Picture1.Height
                TColor = GetPixel(Picture1.hdc, i, j)
                If TColor <> vbWhite Then
                    Call SetPixel(Picture1.hdc, i, j, GetGrayScale(TColor))
                End If
            Next
        Next
    I get the error in the bold part....Im kind of understanding this, but not enough to fix the bugs. Thanks guys for all this help!

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Well you have to copy and paste the original code I posted in my first reply! Make the changes to the hex values and run the code.

  9. #9
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    Don't forget to use Picture1.ScaleWidth instead of Picture1.Width! You also need to set the ScaleMode property of the picturebox to 3(pixels).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width