1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| Public Class LockBitmap
Private source As Bitmap = Nothing
Private Iptr As IntPtr = IntPtr.Zero
Private bitmapData As BitmapData = Nothing
Public Property Pixels() As Byte()
Get
Return m_Pixels
End Get
Set
m_Pixels = Value
End Set
End Property
Private m_Pixels As Byte()
Public Property Depth() As Integer
Get
Return m_Depth
End Get
Private Set
m_Depth = Value
End Set
End Property
Private m_Depth As Integer
Public Property Width() As Integer
Get
Return m_Width
End Get
Private Set
m_Width = Value
End Set
End Property
Private m_Width As Integer
Public Property Height() As Integer
Get
Return m_Height
End Get
Private Set
m_Height = Value
End Set
End Property
Private m_Height As Integer
Public Sub New(source As Bitmap)
Me.source = source
End Sub
''' <summary>
''' Lock bitmap data
''' </summary>
Public Sub LockBits()
Try
' Get width and height of bitmap
Width = source.Width
Height = source.Height
' get total locked pixels count
Dim PixelCount As Integer = Width * Height
' Create rectangle to lock
Dim rect As New Rectangle(0, 0, Width, Height)
' get source bitmap pixel format size
Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat)
' Check if bpp (Bits Per Pixel) is 8, 24, or 32
If Depth <> 8 AndAlso Depth <> 24 AndAlso Depth <> 32 Then
Throw New ArgumentException("Only 8, 24 and 32 bpp images are supported.")
End If
' Lock bitmap and return bitmap data
bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite, source.PixelFormat)
' create byte array to copy pixel values
Dim [step] As Integer = Depth \ 8
Pixels = New Byte(PixelCount * [step] - 1) {}
Iptr = bitmapData.Scan0
' Copy data from pointer to array
Marshal.Copy(Iptr, Pixels, 0, Pixels.Length)
Catch ex As Exception
Throw ex
End Try
End Sub
''' <summary>
''' Unlock bitmap data
''' </summary>
Public Sub UnlockBits()
Try
' Copy data from byte array to pointer
Marshal.Copy(Pixels, 0, Iptr, Pixels.Length)
' Unlock bitmap data
source.UnlockBits(bitmapData)
Catch ex As Exception
Throw ex
End Try
End Sub
''' <summary>
''' Get the color of the specified pixel
''' </summary>
''' <param name="x"></param>
''' <param name="y"></param>
''' <returns></returns>
Public Function GetPixel(x As Integer, y As Integer) As Color
Dim clr As Color = Color.Empty
' Get color components count
Dim cCount As Integer = Depth \ 8
' Get start index of the specified pixel
Dim i As Integer = ((y * Width) + x) * cCount
If i > Pixels.Length - cCount Then
Throw New IndexOutOfRangeException()
End If
If Depth = 32 Then
' For 32 bpp get Red, Green, Blue and Alpha
Dim b As Byte = Pixels(i)
Dim g As Byte = Pixels(i + 1)
Dim r As Byte = Pixels(i + 2)
Dim a As Byte = Pixels(i + 3)
' a
clr = Color.FromArgb(a, r, g, b)
End If
If Depth = 24 Then
' For 24 bpp get Red, Green and Blue
Dim b As Byte = Pixels(i)
Dim g As Byte = Pixels(i + 1)
Dim r As Byte = Pixels(i + 2)
clr = Color.FromArgb(r, g, b)
End If
If Depth = 8 Then
' For 8 bpp get color value (Red, Green and Blue values are the same)
Dim c As Byte = Pixels(i)
clr = Color.FromArgb(c, c, c)
End If
Return clr
End Function
''' <summary>
''' Set the color of the specified pixel
''' </summary>
''' <param name="x"></param>
''' <param name="y"></param>
''' <param name="color"></param>
Public Sub SetPixel(x As Integer, y As Integer, color As Color)
' Get color components count
Dim cCount As Integer = Depth \ 8
' Get start index of the specified pixel
Dim i As Integer = ((y * Width) + x) * cCount
If Depth = 32 Then
' For 32 bpp set Red, Green, Blue and Alpha
Pixels(i) = color.B
Pixels(i + 1) = color.G
Pixels(i + 2) = color.R
Pixels(i + 3) = color.A
End If
If Depth = 24 Then
' For 24 bpp set Red, Green and Blue
Pixels(i) = color.B
Pixels(i + 1) = color.G
Pixels(i + 2) = color.R
End If
If Depth = 8 Then
' For 8 bpp set color value (Red, Green and Blue values are the same)
Pixels(i) = color.B
End If
End Sub
End Class |
Partager