Public Function mask2(ByVal iPic As Bitmap) As Bitmap
' assuming 32 bits per pixel color!!!
Dim NewMask As New Bitmap(iPic.Width, iPic.Height)
Dim i As New Rectangle
i.Width = iPic.Width
i.Height = iPic.Height
Dim sourceData As BitmapData = iPic.LockBits(i, Imaging.ImageLockMode.ReadWrite, iPic.PixelFormat)
Dim NewData As BitmapData = NewMask.LockBits(i, ImageLockMode.ReadWrite, iPic.PixelFormat)
' arrays to store the colors
Dim pixels(i.Width * i.Height - 1) As Integer
Dim pixels2(i.Width * i.Height - 1) As Integer
' copy the data
Marshal.Copy(sourceData.Scan0, pixels, 0, pixels.Length)
Dim index As Integer ' location of pixel(x,y) in pixels array
For y As Integer = 0 To iPic.Height - 1
For x As Integer = 0 To iPic.Width - 1
index = (y * i.Width) + x ' pixels above current row + pixels in current row up to x
If pixels(index) = &HFF000000 Then 'black
pixels2(index) = &HFFFFFFFF ' white
Else
pixels2(index) = &HFF000000 ' black
End If
Next
Next
' Copy data into bitmapdata
Marshal.Copy(pixels2, 0, NewData.Scan0, pixels2.Length)
'unlock
iPic.UnlockBits(sourceData)
NewMask.UnlockBits(NewData)
Return NewMask
End Function