You should avoid color.fromargb and color.toargb too as they are slow.
I did the function like this:
VB Code:
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
If I change the black from &HFF000000 to color.black.toargb and the white similarly, and time it, then it slows down a bit (~4ms instead of ~2ms for a 100,100 bitmap)
When you need the A, R, G, B values then you need to avoid color.toargb. You can do this by using a byte array instead of an int32 array. Or you can use this to split the values from an int:
VB Code:
alpha = (pixels(i) >> 24) And &HFF red = (pixels(i) >> 16) And &HFF green = (pixels(i) >> 8) And &HFF blue = pixels(i) And &HFF
then you will want to write a color back:
VB Code:
pixels(i) = (255 << 24) _ Or (grey << 16) _ Or (grey << 8) _ Or grey




Reply With Quote