GDI+ Brushes, Pens and Alpha Blending in VB.NET

In this article I am going to explain about brushes, pens and alpha blending in GDI+.
  • 10872
 

In GDI+, every color is a combination of ARGB components; each of the alpha, red, green, and blue components is represented by 8 bits. The alpha component in a color structure represents the transparency of the color, which varies from 0 to 255. The value 0 represents full transparency, and 255 represents full opacity.

The final color of an ARGB color structure is calculated by the following formula:


            Final Color = (Source Color x alpha /255) + [Background Color x (255-alpha)/255]


This formula is applied on each component of the source color and background color.

In alpha blending, an application creates a color with an alpha component and uses this color to create a pen or a brush. This pen or brush is used to draw a fill graphics shapes, and it calculates the final color. Alpha blending may sound unfamiliar, but programmatically it is simply a method of setting the alpha component (transparency) of a color, and using it to fill and draw graphics shapes.

Brushes, Pens and Alpha Blending

The process of alpha blending involves three simple steps. First an application creates a color with transparency (the alpha component). The following line creates a Color object with alpha component value 40:


Dim clr As Color = Color.FromArgb(40, 255, 255, 255)

The second step is to create a brush or pen using that color. The following lines create a transparent pen and a brush:


        Dim transPen As New Pen(clr, 10)
        Dim semiTransBrush As New SolidBrush(clr)

Finally, the application uses the transparent brush or pen to fill and draw graphics shapes, lines and curves. The following code uses the Pen and Brush objects we created in the previous steps to draw a line and to draw and fill rectangle:


g.DrawLine(transPen, 10, 30, 200, 30)
g.FillRectangle(semiTransBrush, rect)

Listing 9.31 uses this approach to draw lines, a rectangle, an ellipse, and text object with varying transparency. You can add this code to a menu item or a button click event handler.

LISTING 9.31: Using alpha blending to draw non-opaque or semi-opaque graphics shapes

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Public Class Form1

    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = Me.CreateGraphics()
        g.Clear(Me.BackColor)

        ' Create pens with semitransparent colors
        Dim rect As New Rectangle(220, 30, 100, 50)
        Dim transPen As New Pen(Color.FromArgb(128, 255, 255, 255), 10)
        Dim totTransPen As New Pen(Color.FromArgb(40, 0, 255, 0), 10)

        ' Draw line, rectangle, ellipse, and string using semitransparent colored pens
        g.DrawLine(transPen, 10, 30, 200, 30)
        g.DrawLine(totTransPen, 10, 50, 200, 50)
        g.FillRectangle(New SolidBrush(Color.FromArgb(40, 0, 0, 255)), rect)
        rect.Y += 60
        g.FillEllipse(New SolidBrush(Color.FromArgb(20, 255, 255, 0)), rect)
        Dim semiTransBrush As New SolidBrush(Color.FromArgb(90, 0, 50, 255))
        g.DrawString("Some Photo " & vbLf & "Date: 04/09/2001", New Font("Verdana", 14), semiTransBrush, New RectangleF(20, 100, 300, 100))

        ' Dispose of object
        g.Dispose()
    End Sub
End Class

Figure 9.42 shows the output from Listing 9.31. The lines rectangle, ellipse, and text on this form are semitransparent.

FIGURE-9_42.gif

FIGURE 9.42: Drawing semitransparent graphics shapes

Conclusion

Hope the article would have helped you in understanding Brushes, Pens and Alpha Blending in GDI+. Read other articles on GDI+ on the website.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.