Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'Code originally from http://www.codeproject.com/Tips/240428/Work-with-bitmap-faster-with-Csharp
- Imports System.Drawing
- Imports System.Drawing.Imaging
- Imports System.Runtime.InteropServices
- 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(value As Byte())
- 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(value As Integer)
- 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(value As Integer)
- 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(value As Integer)
- m_Height = value
- End Set
- End Property
- Private m_Height As Integer
- Public Sub New(source As Bitmap)
- Me.source = source
- End Sub
- Public Sub LockBits()
- Try
- Width = source.Width
- Height = source.Height
- Dim PixelCount As Integer = Width * Height
- Dim rect As New Rectangle(0, 0, Width, Height)
- Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat)
- If Depth <> 8 AndAlso Depth <> 24 AndAlso Depth <> 32 _
- Then Throw New ArgumentException("Only 8, 24 and 32 bpp images are supported.")
- bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite, source.PixelFormat)
- Dim [step] As Integer = Depth / 8
- Pixels = New Byte(PixelCount * [step] - 1) {}
- Iptr = bitmapData.Scan0
- Marshal.Copy(Iptr, Pixels, 0, Pixels.Length)
- Catch ex As Exception
- Throw ex
- End Try
- End Sub
- Public Sub UnlockBits()
- Try
- Marshal.Copy(Pixels, 0, Iptr, Pixels.Length)
- source.UnlockBits(bitmapData)
- Catch ex As Exception
- Throw ex
- End Try
- End Sub
- Public Function GetPixel(x As Integer, y As Integer) As Color
- Dim clr As Color = Color.Empty
- Dim cCount As Integer = Depth / 8
- Dim i As Integer = ((y * Width) + x) * cCount
- If i > Pixels.Length - cCount Then Throw New IndexOutOfRangeException()
- If Depth = 32 Then
- 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)
- clr = Color.FromArgb(a, r, g, b)
- End If
- If Depth = 24 Then
- 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
- Dim c As Byte = Pixels(i)
- clr = Color.FromArgb(c, c, c)
- End If
- Return clr
- End Function
- Public Sub SetPixel(x As Integer, y As Integer, color As Color)
- Dim cCount As Integer = Depth / 8
- Dim i As Integer = ((y * Width) + x) * cCount
- If Depth = 32 Then
- 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
- Pixels(i) = color.B
- Pixels(i + 1) = color.G
- Pixels(i + 2) = color.R
- End If
- If Depth = 8 Then Pixels(i) = color.B
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment