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
|
Imports System.Drawing
Imports System.Drawing.Imaging
Public Class BitmapFast
Private b() As Byte
Private i_Width As Integer
Private i_Height As Integer
Private i_Stride As Integer
Private i_ColorSize As Integer
Public Disposed As Boolean
Public Sub New(ByVal filename As String)
If (filename = "") Or (System.IO.File.Exists(filename) = False) Then
MsgBox("file '" & filename & "' not found!")
Exit Sub
End If
Dim pv_bitmap As Bitmap = Nothing
pv_bitmap = New Bitmap(filename)
i_Width = pv_bitmap.Width
i_Height = pv_bitmap.Height
Dim bounds As Rectangle = New Rectangle(0, 0, i_Width, i_Height)
Dim bitmapData As BitmapData = pv_bitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb)
i_Stride = bitmapData.Stride
i_ColorSize = bitmapData.Stride / bitmapData.Width
ReDim b(bitmapData.Height * i_Stride)
System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, b, 0, b.Length - 1)
pv_bitmap.UnlockBits(bitmapData)
pv_bitmap.Dispose()
pv_bitmap = Nothing
End Sub
Public Function Getpixel(ByVal x As Integer, ByVal y As Integer) As Color
Dim red, green, blue As Byte
red = b(x * i_ColorSize + y * i_Stride)
green = b((x * i_ColorSize + 1) + y * i_Stride)
blue = b((x * i_ColorSize + 2) + y * i_Stride)
Return Color.FromArgb(255, red, green, blue)
End Function
Public Sub Setpixel(ByVal x As Integer, ByVal y As Integer, ByVal color As Color)
b(x * i_ColorSize + y * i_Stride) = color.R
b((x * i_ColorSize + 1) + y * i_Stride) = color.G
b((x * i_ColorSize + 2) + y * i_Stride) = color.B
End Sub
Public Function Get_Bitmap() As Bitmap
Get_Bitmap = New Bitmap(i_Width, i_Height, PixelFormat.Format24bppRgb)
Dim bounds As Rectangle = New Rectangle(0, 0, i_Width, i_Height)
Dim bitmapData As Imaging.BitmapData = Get_Bitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb)
bitmapData.Stride = CInt(bitmapData.Stride / i_Width) * i_Width
System.Runtime.InteropServices.Marshal.Copy(b, 0, bitmapData.Scan0, b.Length - 1)
Get_Bitmap.UnlockBits(bitmapData)
End Function
Public Sub Dispose()
If Disposed = True Then Exit Sub
Erase b
Disposed = True
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
If Disposed = False Then Dispose()
End Sub
End Class |
Partager