Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Drawing
- Imports System.Drawing.Imaging
- Imports System.Windows.Forms
- Imports System.Runtime.InteropServices
- Public Class Canvas : Implements IDisposable
- Private Const DEFAULT_BACKGROUND_COLOR As UInteger = &H7F7F7F
- Private memBmp As Bitmap 'Memory bitmap
- Private hMemBmp As IntPtr 'Bitmap handle
- Private memDC As Graphics 'Device context
- Private hMemDC As IntPtr 'Context handle
- Private hOld As IntPtr 'Last selected GDI object
- Private _target As PictureBox 'Render target
- Private _disposed As Boolean = False
- Public Sub New(ByRef Target As PictureBox)
- _target = Target
- CreateContext()
- End Sub
- Friend Sub CreateContext()
- Try
- CleanUp()
- Dim clientDC As Graphics = _target.CreateGraphics()
- Dim hdc As IntPtr = clientDC.GetHdc()
- hMemDC = Win32API.CreateCompatibleDC(hdc)
- memDC = Graphics.FromHdc(hMemDC)
- memBmp = New Bitmap(_target.ClientSize.Width, _target.ClientSize.Height, PixelFormat.Format16bppRgb555)
- hMemBmp = memBmp.GetHbitmap()
- hOld = [Select](hMemBmp)
- Clear()
- clientDC.ReleaseHdc(hdc)
- clientDC.Dispose()
- _disposed = False
- Catch
- End Try
- End Sub
- Friend Function [Select](ByRef hObject As IntPtr) As IntPtr
- Return Win32API.SelectObject(hMemDC, hObject)
- End Function
- Public Sub Resize()
- CreateContext()
- End Sub
- Public Sub Clear(Optional ByVal Color As Integer = DEFAULT_BACKGROUND_COLOR)
- Dim rc As Win32API.STRUCT_RECT
- With rc
- .top = 0
- .left = 0
- .right = _target.ClientSize.Width
- .bottom = _target.ClientSize.Height
- End With
- Dim _brush As New SolidBrush(Color)
- FillRect(hMemDC, rc, _brush.Handle)
- _brush.Dispose()
- End Sub
- Public Sub Render(ByVal e As PaintEventArgs)
- Try
- Dim hdc As IntPtr = e.Graphics.GetHdc()
- Win32API.BitBlt(hdc, e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height, hMemDC, e.ClipRectangle.X, e.ClipRectangle.Y, TernaryRasterOperations.SRCCOPY)
- e.Graphics.ReleaseHdc(hdc)
- Catch
- End Try
- End Sub
- Public Sub Copy(ByRef source As Canvas)
- Try
- Dim hdc As IntPtr = source.Buffer.GetHdc()
- Win32API.BitBlt(hMemDC, 0, 0, source.Target.ClientSize.Width, source.Target.ClientSize.Height, hdc, 0, 0, TernaryRasterOperations.SRCCOPY)
- source.Buffer.ReleaseHdc(hdc)
- Catch
- End Try
- End Sub
- Public ReadOnly Property Graphics As Graphics
- Get
- Return memDC
- End Get
- End Property
- Friend ReadOnly Property Handle As IntPtr
- Get
- Return hMemDC
- End Get
- End Property
- Friend ReadOnly Property Target As PictureBox
- Get
- Return _target
- End Get
- End Property
- Public Overloads Sub Dispose() Implements IDisposable.Dispose
- Dispose(True)
- GC.SuppressFinalize(Me)
- End Sub
- Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
- If Not _disposed Then
- If disposing Then
- 'Clear managed resources
- memBmp.Dispose()
- memDC.Dispose()
- End If
- 'Clear unmanaged resources
- CleanUp(disposing)
- _disposed = True
- End If
- End Sub
- Private Sub CleanUp(Optional ByVal disposing As Boolean = False)
- If hOld.ToInt32 <> 0 Then [Select](hOld)
- If hMemBmp.ToInt32 <> 0 Then Win32API.DeleteObject(hMemBmp)
- If hMemDC.ToInt32 <> 0 Then Win32API.DeleteDC(hMemDC)
- hOld = 0
- hMemBmp = 0
- hMemDC = 0
- If Not disposing Then
- If Not memBmp Is Nothing Then memBmp.Dispose()
- If Not memDC Is Nothing Then memDC.Dispose()
- memDC = Nothing
- memBmp = Nothing
- End If
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement