GraphicsFrameClass

Recently I realized that more and more I am wanting to create buffered graphics when I draw with gdi, vs. the traditional direct use of a graphics object.

I have recently come up with this class that makes things easier. I wanted to share it with the community, so please enjoy.

Example of the GraphicsFrame Class’s Usage:

Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Frame As New GraphicsFrame(Me.Handle, Me.ClientRectangle.Size)
        With Frame.Graphics
            .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            .Clear(Me.BackColor)
            .FillEllipse(Brushes.Red, New Rectangle(10, 10, 100, 100))
            .DrawEllipse(Pens.Green, New Rectangle(10, 10, 100, 100))
            .DrawString("Hello world!", New Font("Consolas", 12), Brushes.Black, New Point(10, 10))
        End With
        Frame.Render()
    End Sub
End Class

The GraphicsFrame Class:

''' <summary>
''' The graphics frame is designed to allow for a quick implementation of buffered graphics.
''' </summary>
''' <remarks></remarks>
Public Class GraphicsFrame
    Private context As System.Drawing.BufferedGraphicsContext
    Private backBuffer As System.Drawing.BufferedGraphics
    Private Property _canvasGraphics As System.Drawing.Graphics
    Private Property _maximumBuffer As System.Drawing.Size
    Public ReadOnly Property Graphics() As System.Drawing.Graphics
        Get
            Return backBuffer.Graphics
        End Get
    End Property
    Public Sub New(ByRef canvas As System.Windows.Forms.Control)
        Me._canvasGraphics = canvas.CreateGraphics
        Me._maximumBuffer = canvas.ClientRectangle.Size
        context = BufferedGraphicsManager.Current
        context.MaximumBuffer = New Size(_maximumBuffer.Width + 1, _maximumBuffer.Height + 1)
        backBuffer = context.Allocate(_canvasGraphics, New Rectangle(0, 0, _maximumBuffer.Width, _maximumBuffer.Height))
    End Sub
    Public Sub New(ByRef canvasGraphics As Graphics, maximumBuffer As Size)
        Me._canvasGraphics = canvasGraphics
        Me._maximumBuffer = maximumBuffer
        context = BufferedGraphicsManager.Current
        context.MaximumBuffer = New Size(_maximumBuffer.Width + 1, _maximumBuffer.Height + 1)
        backBuffer = context.Allocate(_canvasGraphics, New Rectangle(0, 0, _maximumBuffer.Width, _maximumBuffer.Height))
    End Sub
    Public Sub New(ByVal handle As IntPtr, maximumBuffer As Size)
        Me._canvasGraphics = Graphics.FromHwnd(handle)
        Me._maximumBuffer = maximumBuffer
        context = BufferedGraphicsManager.Current
        context.MaximumBuffer = New Size(_maximumBuffer.Width + 1, _maximumBuffer.Height + 1)
        backBuffer = context.Allocate(_canvasGraphics, New Rectangle(0, 0, _maximumBuffer.Width, _maximumBuffer.Height))
    End Sub
    Public Sub New(ByRef canvas As Image)
        Me._canvasGraphics = Graphics.FromImage(canvas)
        Me._maximumBuffer = canvas.Size
        context = BufferedGraphicsManager.Current
        context.MaximumBuffer = New Size(_maximumBuffer.Width + 1, _maximumBuffer.Height + 1)
        backBuffer = context.Allocate(_canvasGraphics, New Rectangle(0, 0, _maximumBuffer.Width, _maximumBuffer.Height))
    End Sub
    Public Sub New(ByRef canvas As Bitmap)
        Me._canvasGraphics = Graphics.FromImage(canvas)
        Me._maximumBuffer = canvas.Size
        context = BufferedGraphicsManager.Current
        context.MaximumBuffer = New Size(_maximumBuffer.Width + 1, _maximumBuffer.Height + 1)
        backBuffer = context.Allocate(_canvasGraphics, New Rectangle(0, 0, _maximumBuffer.Width, _maximumBuffer.Height))
    End Sub
    Public Sub Render()
        If Not backBuffer Is Nothing Then backBuffer.Render()
    End Sub
End Class
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s