VERSION 5.00
Begin VB.Form GetPixelAndSetPixel 
   Caption         =   "Form1"
   ClientHeight    =   3195
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   8670
   LinkTopic       =   "Form1"
   ScaleHeight     =   213
   ScaleMode       =   3  'Pixel
   ScaleWidth      =   578
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton cmdCopyImage 
      Caption         =   "Copy image >>"
      Height          =   495
      Left            =   3240
      TabIndex        =   3
      Top             =   1770
      Width           =   2175
   End
   Begin VB.CommandButton cmdCreateRandom 
      Caption         =   "<< Create Image"
      Height          =   495
      Left            =   3240
      TabIndex        =   2
      Top             =   930
      Width           =   2175
   End
   Begin VB.PictureBox Picture2 
      Height          =   2895
      Left            =   5640
      ScaleHeight     =   189
      ScaleMode       =   3  'Pixel
      ScaleWidth      =   189
      TabIndex        =   1
      Top             =   120
      Width           =   2895
   End
   Begin VB.PictureBox Picture1 
      Height          =   2895
      Left            =   120
      ScaleHeight     =   189
      ScaleMode       =   3  'Pixel
      ScaleWidth      =   189
      TabIndex        =   0
      Top             =   120
      Width           =   2895
   End
End
Attribute VB_Name = "GetPixelAndSetPixel"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long



Private Sub Form_Load()
    Hide
    Picture1.BackColor = vbWhite
    Picture2.BackColor = vbWhite
    Caption = "GetPixel() & SetPixel() Example"
    Show
End Sub


'' Create a random image in Picture1
''
Private Sub cmdCreateRandom_Click()
    Dim i As Long, j As Long, nSize As Long
    Picture1.AutoRedraw = True
    
    '' Picture1 is a square, so width=height, so just cache one value
    ''
    nSize = Picture1.ScaleWidth
    
    For i = 0 To nSize
        '' for each pixel across
        ''
        For j = 0 To nSize
            '' for each pixel down
            '' (i, j) = out i pixels, down j pixels
            ''
            
            SetPixelV Picture1.hdc, i, j, (RGB(i * Rnd, j * Rnd, (i + j) * Rnd))
            
        Next
        
        Picture1.Refresh
    Next
End Sub

'' Copy the image in Picture1 into Picture2
''
Private Sub cmdCopyImage_Click()
    Dim i As Long, j As Long, nSize As Long
    Picture2.AutoRedraw = True
    
    '' Picture2 is a square, so width=height, so just cache one value
    ''
    nSize = Picture2.ScaleWidth
    Picture2.Cls
    
    For i = 0 To nSize
        '' for each pixel across
        ''
        For j = 0 To nSize
            '' for each pixel down
            '' (i, j) = out i pixels, down j pixels
            ''
            
            SetPixelV Picture2.hdc, i, j, GetPixel(Picture1.hdc, i, j)
            
        Next
        
        Picture2.Refresh
    Next
End Sub
