Public Class DC
Implements System.IDisposable
Private Declare Function BitBlt Lib "gdi32" (ByVal hdc As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As CopyPixelOperation) As Boolean
Private Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As IntPtr, ByVal X As Int32, ByVal Y As Int32) As Int32
Private Declare Function SetPixel Lib "gdi32" Alias "SetPixel" (ByVal hdc As IntPtr, ByVal X As Int32, ByVal Y As Int32, ByVal crColor As UInt32) As UInt32
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Boolean
Private Declare Function GetClipBox Lib "gdi32" (ByVal hdc As IntPtr, ByRef lprc As WRECT) As Integer
Private Declare Function GetClipRgn Lib "gdi32" (ByVal hdc As IntPtr, ByRef hrgn As Region) As Integer
Private Declare Function GetRandomRgn Lib "gdi32" (ByVal hdc As IntPtr, ByRef hrgn As Region, ByVal inum As Integer) As Integer
Private managedtype As DCSource
Private managedobject As Object
Private managedgraphics As Graphics
Private hdc As IntPtr
Private _disposed As Boolean
Public Enum DCSource
None
Handle
Graphics
End Enum
Sub New(ByVal g As Graphics)
Me.managedobject = g
Me.managedtype = DCSource.Graphics
Me.hdc = g.GetHdc
End Sub
Sub New(ByVal hwnd As IntPtr, ByVal hdc As IntPtr)
Me.managedobject = hwnd
Me.managedtype = DCSource.Handle
Me.hdc = hdc
End Sub
Sub New()
Me.managedobject = Nothing
Me.managedtype = DCSource.None
End Sub
Public ReadOnly Property Source() As DCSource
Get
Return Me.managedtype
End Get
End Property
Public Sub SetPixel(ByVal X As Integer, ByVal Y As Integer, ByVal C As Color)
SetPixel(Me.hdc, X, Y, C.ToArgb)
End Sub
Public Function GetPixel(ByVal X As Integer, ByVal Y As Integer) As Color
Return ColorTranslator.FromOle(GetPixel(Me.hdc, X, Y))
End Function
Public Property Pixel(ByVal X As Integer, ByVal Y As Integer) As Color
Get
Return GetPixel(X, Y)
End Get
Set(ByVal value As Color)
SetPixel(X, Y, value)
End Set
End Property
Public ReadOnly Property ClipBox() As Rectangle
Get
Dim w As WRECT
GetClipBox(Me.hdc, w)
Return w.Value
End Get
End Property
Public ReadOnly Property ClipSize() As Size
Get
Return Me.ClipBox.Size
End Get
End Property
Public Property Handle() As IntPtr
Get
Return Me.hdc
End Get
Set(ByVal value As IntPtr)
Me.hdc = value
End Set
End Property
Public ReadOnly Property Disposed() As Boolean
Get
Return Me._disposed
End Get
End Property
Public Sub Draw(ByVal source As Graphics, ByVal destrect As Rectangle, ByVal sourceoffset As Point, ByVal operation As CopyPixelOperation)
Dim hdc As IntPtr = source.GetHdc
Draw(hdc, destrect, sourceoffset, operation)
source.ReleaseHdc(hdc)
End Sub
Public Sub Draw(ByVal source As DC, ByVal destrect As Rectangle, ByVal sourceoffset As Point, ByVal operation As CopyPixelOperation)
Draw(source.hdc, destrect, sourceoffset, operation)
End Sub
Public Sub Draw(ByVal sourcehdc As IntPtr, ByVal destrect As Rectangle, ByVal sourceoffset As Point, ByVal operation As CopyPixelOperation)
BitBlt(Me.hdc, destrect.X, destrect.Y, destrect.Width, destrect.Height, sourcehdc, sourceoffset.X, sourceoffset.Y, operation)
End Sub
Public Sub CopyTo(ByVal destination As Graphics, Optional ByVal operation As CopyPixelOperation = CopyPixelOperation.SourceCopy Or CopyPixelOperation.CaptureBlt)
CopyTo(destination, Me.ClipBox, operation)
End Sub
Public Sub CopyTo(ByVal destination As Graphics, ByVal destrect As Rectangle, Optional ByVal operation As CopyPixelOperation = CopyPixelOperation.SourceCopy Or CopyPixelOperation.CaptureBlt)
CopyTo(destination, destrect, New Point(0, 0), operation)
End Sub
Public Sub CopyTo(ByVal destination As Graphics, ByVal destrect As Rectangle, ByVal sourceoffset As Point, Optional ByVal operation As CopyPixelOperation = CopyPixelOperation.SourceCopy Or CopyPixelOperation.CaptureBlt)
Dim hdc As IntPtr = destination.GetHdc
CopyTo(hdc, destrect, sourceoffset, operation)
destination.ReleaseHdc(hdc)
End Sub
Public Sub CopyTo(ByVal destination As DC, Optional ByVal operation As CopyPixelOperation = CopyPixelOperation.SourceCopy Or CopyPixelOperation.CaptureBlt)
CopyTo(destination.hdc, Me.ClipBox, New Point(0, 0), operation)
End Sub
Public Sub CopyTo(ByVal destination As DC, ByVal destrect As Rectangle, Optional ByVal operation As CopyPixelOperation = CopyPixelOperation.SourceCopy Or CopyPixelOperation.CaptureBlt)
CopyTo(destination.hdc, destrect, New Point(0, 0), operation)
End Sub
Public Sub CopyTo(ByVal destination As DC, ByVal destrect As Rectangle, ByVal sourceoffset As Point, Optional ByVal operation As CopyPixelOperation = CopyPixelOperation.SourceCopy Or CopyPixelOperation.CaptureBlt)
CopyTo(destination.hdc, destrect, sourceoffset, operation)
End Sub
Public Sub CopyTo(ByVal desthdc As IntPtr, ByVal destrect As Rectangle, ByVal sourceoffset As Point, Optional ByVal operation As CopyPixelOperation = CopyPixelOperation.SourceCopy Or CopyPixelOperation.CaptureBlt)
BitBlt(desthdc, destrect.X, destrect.Y, destrect.Width, destrect.Height, Me.hdc, sourceoffset.X, sourceoffset.Y, operation)
End Sub
Public Function ToBitmap(Optional ByVal operation As CopyPixelOperation = CopyPixelOperation.SourceCopy Or CopyPixelOperation.CaptureBlt) As Bitmap
Return ToBitmap(Me.ClipSize, operation)
End Function
Public Function ToBitmap(ByVal Resolution As Size, Optional ByVal operation As CopyPixelOperation = CopyPixelOperation.SourceCopy Or CopyPixelOperation.CaptureBlt) As Bitmap
ToBitmap = New Bitmap(Resolution.Width, Resolution.Height)
Dim g As Graphics = Graphics.FromImage(ToBitmap)
Dim ghdc As IntPtr = g.GetHdc
BitBlt(ghdc, 0, 0, Resolution.Width, Resolution.Height, Me.hdc, 0, 0, operation)
g.ReleaseHdc(ghdc)
g.Dispose()
End Function
Public ReadOnly Property Graphics() As Graphics
Get
If Me.managedgraphics Is Nothing Then Me.managedgraphics = Graphics.FromHdc(Me.hdc)
Return Me.managedgraphics
End Get
End Property
Public Shared Function FromHandle(ByVal handle As IntPtr) As DC
FromHandle = New DC()
FromHandle.hdc = handle
End Function
Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
If Me._disposed = False Then
Select Case managedtype
Case DCSource.Handle
ReleaseDC(CType(Me.managedobject, IntPtr), Me.hdc)
Case DCSource.Graphics
CType(Me.managedobject, Graphics).ReleaseHdc(Me.hdc)
End Select
If managedgraphics IsNot Nothing Then managedgraphics.Dispose()
Me.managedgraphics = Nothing
Me.managedobject = Nothing
Me.managedtype = Nothing
Me.hdc = Nothing
Me._disposed = True
End If
End Sub
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
End Class