1. Home
  2. Computing & Technology
  3. Visual Basic

GDI+ Graphics in Visual Basic 2005 .NET
Source Code for the Temperature Guage Gradient

By Dan Mabbutt, About.com

Mar 4 2007

To help you put it all together, the entire program source follows. To use this code, you need to name your form BlendForm and add an HScrollBar component and name it BlendScroll. You will also need two image files.

Imports System.Drawing.Drawing2D
Public Class BlendForm
   Private PositionArray(2) As Single
   Private FactorArray(2) As Single
   Private i1 As Image = _
      Image.FromFile("C:\image1.gif")
   Private i2 As Image = _
      Image.FromFile("C:\image2.gif")
   Private i As Image = i1
   Private StartRecX As Integer = _
      CInt((Me.Width - i.Width) / 2)
   Private StartRecY As Integer = _
      CInt(((Me.Height - i.Height) / 2) - 20)
   Protected Overrides Sub OnPaint( _
      ByVal e As System.Windows.Forms.PaintEventArgs)
      Dim g As Graphics = e.Graphics
      Using myBrush As LinearGradientBrush _
         = New LinearGradientBrush( _
         Me.ClientRectangle, _
         Color.Blue, Color.Red, _
         LinearGradientMode.Horizontal)
         Dim myBlend As Blend = New Blend(2)
         myBlend.Factors = FactorArray
         myBlend.Positions = PositionArray
         myBrush.Blend = myBlend
         g.FillRectangle( _
            myBrush, Me.ClientRectangle)
         g.DrawImage( _
            i, StartRecX, StartRecY, i.Width, i.Height)
         MyBase.OnPaint(e)
      End Using
   End Sub
   Private Sub BlendScroll_Scroll( _
      ByVal sender As System.Object, _
      ByVal e As System.Windows.Forms.ScrollEventArgs) _
      Handles BlendScroll.Scroll
      PositionArray(1) = BlendScroll.Value / 91
      ' adjustment to compensate for bug in scroll control
      ' see http://visualbasic.about.com/b/a/256812.htm for details
      If PositionArray(1) < 0.5 Then
         i = i1
      Else
         i = i2
      End If
      Me.Invalidate()
   End Sub
   Private Sub BlendForm_Load( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles MyBase.Load
      FactorArray = New Single() {0.0, 0.5, 1.0}
      PositionArray = New Single() {0.0, 0.5, 1.0}
      SetStyle(ControlStyles.AllPaintingInWmPaint Or _
         ControlStyles.OptimizedDoubleBuffer Or _
         ControlStyles.UserPaint, True)
   End Sub
End Class

Part 5 of the tutorial explains the concept of a graphics coordinate space. A coordinate space is a key element for understanding graphic transformations using the Matrix object that is also featured in Part 5.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. GDI+ Graphics in Visual Basic 2005 .NET

©2009 About.com, a part of The New York Times Company.

All rights reserved.