I have 3 hindsight notes on the Inverter Class:
1. The class returns a new instance of a bitmap on every call to Process( ),
this is slow and dumb, and defeats the purpose of working directly on the
image bits.
2. There is no reason for this class to implement IDisposable, or to hold
state (m_bmpImage) like it does. I added this for something else.
3. The entire class could be easily summed up into a single function that
takes a Bitmap as a parameter and operates directly on it, with no return
value.
For anyone who didn't get the attachment:
(watch for line breaks)
__________________________________________
Public Class Inverter
Implements IDisposable
Private m_bmpImage As Bitmap
Private Disposed As Boolean = False
Public Overloads Sub Dispose() Implements System.IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
Me.Dispose(False)
MyBase.Finalize()
End Sub
Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
If Not (Me.Disposed) Then
If (disposing) Then
'managed components
Me.Image.Dispose()
End If
'unmanaged
End If
Me.Disposed = True
End Sub
Public Property Image() As Bitmap
Get
Return m_bmpImage
End Get
Set(ByVal Value As Bitmap)
'// convert the image format
Dim g As Graphics
If Not Me.m_bmpImage Is Nothing Then
Me.m_bmpImage.Dispose()
End If
Me.m_bmpImage = New Bitmap(Value.Width, Value.Height,
Drawing.Imaging.PixelFormat.Format32bppRgb)
g = Graphics.FromImage(Me.m_bmpImage)
g.DrawImage(Value, 0, 0)
g.Dispose()
End Set
End Property
Public Function Process() As Image
Dim bts As Drawing.Imaging.BitmapData
Dim b() As Byte
Dim Y, iBDIndex, X As Integer
ReDim b((Image.Width * 4) * (Image.Height))
bts = Image.LockBits(New Rectangle(0, 0, Image.Width, Image.Height),
Drawing.Imaging.ImageLockMode.ReadWrite,
Drawing.Imaging.PixelFormat.Format32bppArgb)
Runtime.InteropServices.Marshal.Copy(bts.Scan0, b, 0, ((Image.Width)
* 4) * (Image.Height))
iBDIndex = 0
For Y = 0 To (Image.Height - 1)
For X = 0 To ((UBound(b) / 4) / (Image.Height)) - 1
b(iBDIndex) = Not b(iBDIndex)
b(iBDIndex + 1) = Not b(iBDIndex + 1)
b(iBDIndex + 2) = Not b(iBDIndex + 2)
iBDIndex += 4
Next
Next
Runtime.InteropServices.Marshal.Copy(b, 0, bts.Scan0, ((Image.Width)
* 4) * (Image.Height))
Image.UnlockBits(bts)
Return New Bitmap(Image)
End Function
End Class