Results 1 to 6 of 6

Thread: Painting a picture box

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Location
    Liverpool UK
    Posts
    23

    Question

    I've got a module that scans a given pixel of screen and returns it's RGB color.

    Now what I want to do is to be able to change the pixel color of a given pixel on the screen.

    So lets say I have 2 Picture boxes A and B. I can scan PicBoxA's RGB colors for each pixel. What I need to know is how I could replicate that same pixel in PicBox B.

    Basically, I want to know how to paint a picture box's pixels different colors.

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    Im not sure how, but dennis is making a paint program and might be able to help you

  3. #3
    Guest
    Try the following:

    Code:
    Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
    Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Long
    
    Private Sub Command1_Click()
    
        'Loop through each Pixel
        For X = 0 To Picture1.Width
            For Y = 1 To Picture1.Height
                'Get the Pixel from Picture1
                crColour = GetPixel(Picture1.hdc, X, Y)
    
                '<--Do Pixel manipulation here-->
    
                'Draw the pixel on Picture2
                SetPixel Picture2.hdc, X, Y, crColor
            Next Y
        Next X
        
    End Sub

  4. #4
    Guest
    You can use SetPixel or BitBlt.

    and if you want to exactly dupe the picture, you can use 2 for loops, one inside the other and use GetPixel and SetPixel to get PicA's pixel and set PicB's pixel.


    here I just wrote some sample code....

    you need 2 picture boxes and a command button.
    and be warned this code is a little slow!

    Code:
    Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
    Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Long
    
    Private Sub Command1_Click()
    
    Dim i As Long
    Dim j As Long
    Dim dPixel As Long
    
    For i = 0 To Picture1.Width
     For j = 0 To Picture1.Height
      dPixel = GetPixel(Picture1.hdc, i, j)
      SetPixel Picture2.hdc, i, j, dPixel
     Next
    Next
    
    
    End Sub
    
    Private Sub Form_Load()
    
     Me.ScaleMode = vbPixels
     Picture1.ScaleMode = vbPixels
     Picture2.ScaleMode = vbPixels
    
    End Sub
    
    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     
     If Button = 1 Then
      SetPixel Picture1.hdc, X, Y, vbRed
      SetPixel Picture1.hdc, X + 1, Y + 1, vbGreen
      SetPixel Picture1.hdc, X + 2, Y + 2, vbYellow
     End If
    
    End Sub

  5. #5
    Guest
    it took me ten minutes to write that!!!!!
    it didnt seem like that long..... time flies when your having fun (writing code)

  6. #6
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Indeed, hours become minutes when coding (Have experience in that )

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