TizzyT

LockBitmap

Sep 5th, 2015
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.83 KB | None | 0 0
  1. 'Code originally from http://www.codeproject.com/Tips/240428/Work-with-bitmap-faster-with-Csharp
  2. Imports System.Drawing
  3. Imports System.Drawing.Imaging
  4. Imports System.Runtime.InteropServices
  5. Public Class LockBitmap
  6.     Private source As Bitmap = Nothing
  7.     Private Iptr As IntPtr = IntPtr.Zero
  8.     Private bitmapData As BitmapData = Nothing
  9.     Public Property Pixels() As Byte()
  10.         Get
  11.             Return m_Pixels
  12.         End Get
  13.         Set(value As Byte())
  14.             m_Pixels = value
  15.         End Set
  16.     End Property
  17.     Private m_Pixels As Byte()
  18.     Public Property Depth() As Integer
  19.         Get
  20.             Return m_Depth
  21.         End Get
  22.         Private Set(value As Integer)
  23.             m_Depth = value
  24.         End Set
  25.     End Property
  26.     Private m_Depth As Integer
  27.     Public Property Width() As Integer
  28.         Get
  29.             Return m_Width
  30.         End Get
  31.         Private Set(value As Integer)
  32.             m_Width = value
  33.         End Set
  34.     End Property
  35.     Private m_Width As Integer
  36.     Public Property Height() As Integer
  37.         Get
  38.             Return m_Height
  39.         End Get
  40.         Private Set(value As Integer)
  41.             m_Height = value
  42.         End Set
  43.     End Property
  44.     Private m_Height As Integer
  45.     Public Sub New(source As Bitmap)
  46.         Me.source = source
  47.     End Sub
  48.     Public Sub LockBits()
  49.         Try
  50.             Width = source.Width
  51.             Height = source.Height
  52.             Dim PixelCount As Integer = Width * Height
  53.             Dim rect As New Rectangle(0, 0, Width, Height)
  54.             Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat)
  55.             If Depth <> 8 AndAlso Depth <> 24 AndAlso Depth <> 32 _
  56.             Then Throw New ArgumentException("Only 8, 24 and 32 bpp images are supported.")
  57.             bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite, source.PixelFormat)
  58.             Dim [step] As Integer = Depth / 8
  59.             Pixels = New Byte(PixelCount * [step] - 1) {}
  60.             Iptr = bitmapData.Scan0
  61.             Marshal.Copy(Iptr, Pixels, 0, Pixels.Length)
  62.         Catch ex As Exception
  63.             Throw ex
  64.         End Try
  65.     End Sub
  66.     Public Sub UnlockBits()
  67.         Try
  68.             Marshal.Copy(Pixels, 0, Iptr, Pixels.Length)
  69.             source.UnlockBits(bitmapData)
  70.         Catch ex As Exception
  71.             Throw ex
  72.         End Try
  73.     End Sub
  74.     Public Function GetPixel(x As Integer, y As Integer) As Color
  75.         Dim clr As Color = Color.Empty
  76.         Dim cCount As Integer = Depth / 8
  77.         Dim i As Integer = ((y * Width) + x) * cCount
  78.         If i > Pixels.Length - cCount Then Throw New IndexOutOfRangeException()
  79.         If Depth = 32 Then
  80.             Dim b As Byte = Pixels(i)
  81.             Dim g As Byte = Pixels(i + 1)
  82.             Dim r As Byte = Pixels(i + 2)
  83.             Dim a As Byte = Pixels(i + 3)
  84.             clr = Color.FromArgb(a, r, g, b)
  85.         End If
  86.         If Depth = 24 Then
  87.             Dim b As Byte = Pixels(i)
  88.             Dim g As Byte = Pixels(i + 1)
  89.             Dim r As Byte = Pixels(i + 2)
  90.             clr = Color.FromArgb(r, g, b)
  91.         End If
  92.         If Depth = 8 Then
  93.             Dim c As Byte = Pixels(i)
  94.             clr = Color.FromArgb(c, c, c)
  95.         End If
  96.         Return clr
  97.     End Function
  98.     Public Sub SetPixel(x As Integer, y As Integer, color As Color)
  99.         Dim cCount As Integer = Depth / 8
  100.         Dim i As Integer = ((y * Width) + x) * cCount
  101.         If Depth = 32 Then
  102.             Pixels(i) = color.B
  103.             Pixels(i + 1) = color.G
  104.             Pixels(i + 2) = color.R
  105.             Pixels(i + 3) = color.A
  106.         End If
  107.         If Depth = 24 Then
  108.             Pixels(i) = color.B
  109.             Pixels(i + 1) = color.G
  110.             Pixels(i + 2) = color.R
  111.         End If
  112.         If Depth = 8 Then Pixels(i) = color.B
  113.     End Sub
  114. End Class
Advertisement
Add Comment
Please, Sign In to add comment