Advertisement
Guest User

Sprite.vb

a guest
Feb 26th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.58 KB | None | 0 0
  1. Option Explicit On
  2.  
  3. Imports System.Drawing
  4. Imports System.Runtime.InteropServices
  5.  
  6. Public Class Sprite : Implements IDisposable
  7.  
  8.     Private Const COLOR_WHITE As Long = &HFFFFFF
  9.     Private Const COLOR_BLACK As Long = &H0
  10.     Private Const COLOR_MAGENTA As Long = &HFF00FF
  11.     Private Const DEFAULT_MASK_COLOR As Long = COLOR_MAGENTA
  12.  
  13.     Private _bitmap As CUSTOM_BITMAP
  14.     Private _mask As CUSTOM_BITMAP
  15.     Private _hDC As IntPtr = IntPtr.Zero
  16.     Private _hBmp As IntPtr = IntPtr.Zero
  17.     Private _hOld As IntPtr = IntPtr.Zero
  18.     Private _location As PointF
  19.     Private _disposed As Boolean = False
  20.  
  21.     Public Sub New(ByVal Filename As String, ByRef g As Graphics, Optional ByVal TransparentColor As Integer = DEFAULT_MASK_COLOR)
  22.         CleanUp(_bitmap)
  23.         _bitmap = LoadBitmap(Filename)
  24.         CleanUp(_mask, True)
  25.         _mask = CreateMask(Filename, TransparentColor)
  26.         Dim gdc As IntPtr = g.GetHdc()
  27.         _hBmp = CreateCompatibleBitmap(gdc, _bitmap.Info.bmWidth, _bitmap.Info.bmHeight)
  28.         _hDC = CreateCompatibleDC(gdc)
  29.         _hOld = SelectObject(_hDC, _hBmp)
  30.         Win32API.BitBlt(_hDC, 0, 0, _bitmap.Info.bmWidth, _bitmap.Info.bmHeight, gdc, 0, 0, TernaryRasterOperations.SRCCOPY)
  31.         g.ReleaseHdc()
  32.         _disposed = False
  33.     End Sub
  34.     Private Function LoadBitmap(ByVal Filename As String) As CUSTOM_BITMAP
  35.         Dim bmp As CUSTOM_BITMAP
  36.  
  37.         With bmp
  38.             Try
  39.                 .hBmp = Win32API.LoadImage(0, Filename, 0, 0, 0, LR_LOADFROMFILE)
  40.                 .hDC = CreateCompatibleDC(0)
  41.                 Win32API.SelectObject(.hDC, .hBmp)
  42.                 Dim hSDBmp As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(.Info))
  43.                 Marshal.StructureToPtr(.Info, hSDBmp, False)
  44.                 Win32API.GetObject(.hBmp, Len(.Info), hSDBmp)
  45.                 .Info = CType(Marshal.PtrToStructure(hSDBmp, GetType(Win32API.STRUCT_BITMAP)), Win32API.STRUCT_BITMAP)
  46.                 .Info = .Info
  47.                 Marshal.FreeHGlobal(hSDBmp)
  48.                 .hOld = Win32API.SelectObject(.hDC, .hBmp)
  49.             Catch
  50.             End Try
  51.         End With
  52.  
  53.         Return bmp
  54.     End Function
  55.     Private Function CreateMask(ByVal Filename As String, ByVal ColorKey As Integer) As CUSTOM_BITMAP
  56.         Dim bmpMask As CUSTOM_BITMAP
  57.         Dim px As Integer
  58.         Dim py As Integer
  59.         Dim pc As Integer
  60.  
  61.         bmpMask = LoadBitmap(Filename)
  62.         With bmpMask
  63.             Try
  64.                 For py = 0 To .Info.bmHeight - 1
  65.                     For px = 0 To .Info.bmWidth - 1
  66.                         pc = GetPixel(.hDC, px, py)
  67.                         If ColorKey = pc Then
  68.                             SetPixel(.hDC, px, py, COLOR_WHITE) 'Not visible
  69.                         Else
  70.                             SetPixel(.hDC, px, py, COLOR_BLACK) 'Visible
  71.                         End If
  72.                     Next px
  73.                 Next py
  74.             Catch
  75.             End Try
  76.         End With
  77.  
  78.         Return bmpMask
  79.     End Function
  80.     Public Sub Render(ByRef target As Canvas, Optional ByVal UseTransparency As Boolean = False)
  81.         Render(target.Handle, _location.X, _location.Y, UseTransparency)
  82.     End Sub
  83.     Public Sub Render(ByRef target As Canvas, ByVal X As Integer, ByVal Y As Integer, Optional ByVal UseTransparency As Boolean = False)
  84.         Render(target.Handle, X, Y, UseTransparency)
  85.     End Sub
  86.     Friend Sub Render(ByVal hdcDest As IntPtr, Optional ByVal UseTransparency As Boolean = False)
  87.         Render(hdcDest, 0, 0, UseTransparency)
  88.     End Sub
  89.     Friend Sub Render(ByVal hdcDest As IntPtr, ByVal X As Integer, ByVal Y As Integer, Optional ByVal UseTransparency As Boolean = False)
  90.         If UseTransparency Then
  91.             Win32API.BitBlt(_hDC, 0, 0, _bitmap.Info.bmWidth, _bitmap.Info.bmHeight, _mask.hDC, 0, 0, TernaryRasterOperations.SRCAND) 'Draw the mask
  92.             Win32API.BitBlt(_hDC, 0, 0, _bitmap.Info.bmWidth, _bitmap.Info.bmHeight, _bitmap.hDC, 0, 0, TernaryRasterOperations.SRCPAINT) 'Overlay the image
  93.         Else
  94.             Win32API.BitBlt(_hDC, 0, 0, _bitmap.Info.bmWidth, _bitmap.Info.bmHeight, _bitmap.hDC, 0, 0, TernaryRasterOperations.SRCCOPY) 'Just draw the plain image
  95.         End If
  96.         Win32API.BitBlt(hdcDest, X, Y, _bitmap.Info.bmWidth, _bitmap.Info.bmHeight, _hDC, 0, 0, TernaryRasterOperations.SRCCOPY) 'Render results to the destination
  97.     End Sub
  98.  
  99.     Public Property Location As PointF
  100.         Get
  101.             Return _location
  102.         End Get
  103.         Set(ByVal value As PointF)
  104.             _location = value
  105.         End Set
  106.     End Property
  107.     Public Property X As Single
  108.         Get
  109.             Return _location.X
  110.         End Get
  111.         Set(ByVal value As Single)
  112.             _location.X = value
  113.         End Set
  114.     End Property
  115.     Public Property Y As Single
  116.         Get
  117.             Return _location.Y
  118.         End Get
  119.         Set(ByVal value As Single)
  120.             _location.Y = value
  121.         End Set
  122.     End Property
  123.     Public ReadOnly Property Width As Integer
  124.         Get
  125.             Return _bitmap.Info.bmWidth
  126.         End Get
  127.     End Property
  128.     Public ReadOnly Property Height As Integer
  129.         Get
  130.             Return _bitmap.Info.bmHeight
  131.         End Get
  132.     End Property
  133.     Public ReadOnly Property Size As Size
  134.         Get
  135.             Return New Size(Width, Height)
  136.         End Get
  137.     End Property
  138.  
  139.     Public Overloads Sub Dispose() Implements IDisposable.Dispose
  140.         Dispose(True)
  141.         GC.SuppressFinalize(Me)
  142.     End Sub
  143.     Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
  144.         If Not _disposed Then
  145.             'Clear unmanaged resources
  146.             CleanUp(_bitmap)
  147.             CleanUp(_mask, True)
  148.             _disposed = True
  149.         End If
  150.     End Sub
  151.     Private Sub CleanUp(ByRef bmp As CUSTOM_BITMAP, Optional ByVal DeleteContext As Boolean = False)
  152.         With bmp
  153.             If .hOld.ToInt32 <> 0 Then Win32API.SelectObject(.hDC, .hOld)
  154.             If .hBmp.ToInt32 <> 0 Then Win32API.DeleteObject(.hBmp)
  155.             If .hDC.ToInt32 <> 0 Then Win32API.DeleteDC(.hDC)
  156.             If _hdc.ToInt32 <> 0 And DeleteContext Then Win32API.DeleteDC(_hdc)
  157.             .hOld = 0
  158.             .hBmp = 0
  159.             .hDC = 0
  160.             If DeleteContext Then _hdc = IntPtr.Zero
  161.             With .Info
  162.                 .bmBits = 0
  163.                 .bmBitsPixel = 0
  164.                 .bmHeight = 0
  165.                 .bmPlanes = 0
  166.                 .bmType = 0
  167.                 .bmWidth = 0
  168.                 .bmWidthBytes = 0
  169.             End With
  170.         End With
  171.     End Sub
  172.  
  173. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement