GDI+ Alpha Blending and Images in VB.NET

In this article I will explain about Alpha Blending and Images in GDI+.
  • 2113
Download Files:
 

We often see a semitransparent date and place name on a photo. You can draw transparent graphics shapes on images using the same method: Create a graphics shape using semi- or non-opaque colors, and then draw on the image.

Listing 9.32 draws graphics shapes on an image. First we create an Image object and call DrawImage to draw an image. Then we create transparent pens and brushes and call fill and draw methods to draw graphics shapes. You can add the code in Listing 9.32 to any menu item or button click event handler.

LISTING 9.32: Drawing semitransparent graphics shapes on an image


 

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)

 

        ' Draw an image

    

   g.DrawImage(curImage, 0, 0, curImage.Width, curImage.Height)

 

        ' Create pens and a rectangle

        Dim rect As New Rectangle(220, 30, 100, 50)

        Dim opqPen As New Pen(Color.FromArgb(128, 255, 255, 255), 10)

        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 lines, rectangle, ellipse, and string using

        g.DrawLine(opqPen, 10, 10, 200, 10)

        g.DrawLine(transpen, 10, 30, 200, 30)

        g.DrawLine(totTransPen, 10, 50, 200, 50)

        g.FillRectangle(New SolidBrush(Color.FromArgb(140, 0, 0, 255)), rect)

        rect.Y += 60

        g.FillEllipse(New SolidBrush(Color.FromArgb(150, 255, 255, 255)), rect)

        Dim semiTransBrush As New SolidBrush(Color.FromArgb(90, 255, 255, 50))

        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 ClassFigure 9.43 shows the output from Listing 9.32. Lines, text, a rectangle, and an ellipse are drawn on top of the image, but you can see through them because these shapes are semitransparent.

alpha.gif

FIGURE 9.43: Drawing semitransparent shapes on an image

Conclusion

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

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.