Public Class GDIWrapper Implements IDisposable Private Declare Function ExtFloodFill Lib "gdi32" (ByVal hdc As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal crColor As Integer, ByVal wFillType As Integer) As Integer Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Integer) As IntPtr Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As IntPtr, ByVal X As Integer, ByVal Y As Integer) As Integer Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As IntPtr, ByVal hObject As IntPtr) As IntPtr Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As IntPtr) As Integer Private Declare Function GdiFlush Lib "gdi32" Alias "GdiFlush" () As Integer Const PS_SOLID As Integer = 0 Private Declare Function CreatePen Lib "gdi32" (ByVal fnPenStyle As Integer, ByVal nWidth As Integer, ByVal crColor As Integer) As IntPtr Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal lpPoint As Integer) As Boolean Private Declare Function LineTo Lib "gdi32" (ByVal hdc As IntPtr, ByVal nXEnd As Integer, ByVal nYEnd As Integer) As Boolean Private Const FLOODFILLBORDER As Integer = 0 Private Const FLOODFILLSURFACE As Integer = 1 Protected mainGraphics As Graphics Protected hDC As IntPtr Public Sub New(image As Bitmap) mainGraphics = Graphics.FromImage(image) hDC = mainGraphics.GetHdc End Sub Public Sub Dispose() Implements IDisposable.Dispose mainGraphics.ReleaseHdc() mainGraphics.Dispose() End Sub Private Sub CreatePen(color As Color) '新しいペンを作成 Dim hPen As IntPtr hPen = CreatePen(PS_SOLID, 2, ColorTranslator.ToOle(color)) Dim hOldPen As IntPtr hOldPen = SelectObject(hDC, hPen) DeleteObject(hOldPen) End Sub Private Sub CreateBrush(color As Color) '新しいブラシを作成 Dim hBrush As IntPtr hBrush = CreateSolidBrush(ColorTranslator.ToOle(color)) Dim hOldBrush As IntPtr hOldBrush = SelectObject(hDC, hBrush) DeleteObject(hOldBrush) End Sub Public Sub DrawLine(pen As Pen, x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer) Me.DrawLine(pen, New Point(x1, y1), New Point(x2, y2)) End Sub Public Sub DrawLine(pen As Pen, pt1 As Point, pt2 As Point) CreatePen(pen.Color) MoveToEx(hDC, pt1.X, pt1.Y, 0) LineTo(hDC, pt2.X, pt2.Y) End Sub Public Sub FloodFill(color As Color, pt As Point) CreateBrush(color) Dim backColor As Integer = GetPixel(hDC, pt.X, pt.Y) ExtFloodFill(hDC, pt.X, pt.Y, backColor, FLOODFILLSURFACE) End Sub Public Sub Flush() GdiFlush() End Sub End Class