ARTICLE

Alpha Blending and Images in GDI+

Posted by Dinesh Beniwal Articles | GDI+ in VB.NET July 06, 2010
In this article I will explain about Alpha Blending and Images in GDI+.
Download Files:
 
Reader Level:

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 Class

Figure 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.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Diagram
Become a Sponsor