Advertisement
Guest User

Canvas.vb

a guest
Feb 26th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.14 KB | None | 0 0
  1. Imports System.Drawing
  2. Imports System.Drawing.Imaging
  3. Imports System.Windows.Forms
  4. Imports System.Runtime.InteropServices
  5.  
  6. Public Class Canvas : Implements IDisposable
  7.  
  8.     Private Const DEFAULT_BACKGROUND_COLOR As UInteger = &H7F7F7F
  9.  
  10.     Private memBmp As Bitmap        'Memory bitmap
  11.     Private hMemBmp As IntPtr       'Bitmap handle
  12.     Private memDC As Graphics       'Device context
  13.     Private hMemDC As IntPtr        'Context handle
  14.     Private hOld As IntPtr          'Last selected GDI object
  15.     Private _target As PictureBox   'Render target
  16.     Private _disposed As Boolean = False
  17.  
  18.     Public Sub New(ByRef Target As PictureBox)
  19.         _target = Target
  20.         CreateContext()
  21.     End Sub
  22.     Friend Sub CreateContext()
  23.         Try
  24.             CleanUp()
  25.  
  26.             Dim clientDC As Graphics = _target.CreateGraphics()
  27.             Dim hdc As IntPtr = clientDC.GetHdc()
  28.             hMemDC = Win32API.CreateCompatibleDC(hdc)
  29.             memDC = Graphics.FromHdc(hMemDC)
  30.  
  31.             memBmp = New Bitmap(_target.ClientSize.Width, _target.ClientSize.Height, PixelFormat.Format16bppRgb555)
  32.             hMemBmp = memBmp.GetHbitmap()
  33.             hOld = [Select](hMemBmp)
  34.             Clear()
  35.  
  36.             clientDC.ReleaseHdc(hdc)
  37.             clientDC.Dispose()
  38.             _disposed = False
  39.         Catch
  40.         End Try
  41.     End Sub
  42.     Friend Function [Select](ByRef hObject As IntPtr) As IntPtr
  43.         Return Win32API.SelectObject(hMemDC, hObject)
  44.     End Function
  45.     Public Sub Resize()
  46.         CreateContext()
  47.     End Sub
  48.     Public Sub Clear(Optional ByVal Color As Integer = DEFAULT_BACKGROUND_COLOR)
  49.         Dim rc As Win32API.STRUCT_RECT
  50.         With rc
  51.             .top = 0
  52.             .left = 0
  53.             .right = _target.ClientSize.Width
  54.             .bottom = _target.ClientSize.Height
  55.         End With
  56.         Dim _brush As New SolidBrush(Color)
  57.         FillRect(hMemDC, rc, _brush.Handle)
  58.         _brush.Dispose()
  59.     End Sub
  60.     Public Sub Render(ByVal e As PaintEventArgs)
  61.         Try
  62.             Dim hdc As IntPtr = e.Graphics.GetHdc()
  63.             Win32API.BitBlt(hdc, e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height, hMemDC, e.ClipRectangle.X, e.ClipRectangle.Y, TernaryRasterOperations.SRCCOPY)
  64.             e.Graphics.ReleaseHdc(hdc)
  65.         Catch
  66.         End Try
  67.     End Sub
  68.     Public Sub Copy(ByRef source As Canvas)
  69.         Try
  70.             Dim hdc As IntPtr = source.Buffer.GetHdc()
  71.             Win32API.BitBlt(hMemDC, 0, 0, source.Target.ClientSize.Width, source.Target.ClientSize.Height, hdc, 0, 0, TernaryRasterOperations.SRCCOPY)
  72.             source.Buffer.ReleaseHdc(hdc)
  73.         Catch
  74.         End Try
  75.     End Sub
  76.  
  77.     Public ReadOnly Property Graphics As Graphics
  78.         Get
  79.             Return memDC
  80.         End Get
  81.     End Property
  82.     Friend ReadOnly Property Handle As IntPtr
  83.         Get
  84.             Return hMemDC
  85.         End Get
  86.     End Property
  87.     Friend ReadOnly Property Target As PictureBox
  88.         Get
  89.             Return _target
  90.         End Get
  91.     End Property
  92.  
  93.     Public Overloads Sub Dispose() Implements IDisposable.Dispose
  94.         Dispose(True)
  95.         GC.SuppressFinalize(Me)
  96.     End Sub
  97.     Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
  98.         If Not _disposed Then
  99.             If disposing Then
  100.                 'Clear managed resources
  101.                 memBmp.Dispose()
  102.                 memDC.Dispose()
  103.             End If
  104.             'Clear unmanaged resources
  105.             CleanUp(disposing)
  106.             _disposed = True
  107.         End If
  108.     End Sub
  109.     Private Sub CleanUp(Optional ByVal disposing As Boolean = False)
  110.         If hOld.ToInt32 <> 0 Then [Select](hOld)
  111.         If hMemBmp.ToInt32 <> 0 Then Win32API.DeleteObject(hMemBmp)
  112.         If hMemDC.ToInt32 <> 0 Then Win32API.DeleteDC(hMemDC)
  113.         hOld = 0
  114.         hMemBmp = 0
  115.         hMemDC = 0
  116.         If Not disposing Then
  117.             If Not memBmp Is Nothing Then memBmp.Dispose()
  118.             If Not memDC Is Nothing Then memDC.Dispose()
  119.             memDC = Nothing
  120.             memBmp = Nothing
  121.         End If
  122.     End Sub
  123.  
  124. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement