Graphics Methodology – 2

Graphics Methodology – 2

There are many ways to handle graphics tasks in VB .Net, but it’s not always obvious which approach is best for a particular task. Some different approaches are discussed here.

The sorts of tasks that will be considered here include:
– Drawing an image on a form
  – Displaying an image from a file on a form or in a picturebox
– Drawing shapes on a form or picturebox
– Simple animation.

Previous…

The simplest way to draw an image on a form or on a control is to to set an appropriate property of the form or control to the image file. This can be done from the designer or from code.  For instance:

Me.BackgroundImage = Image.FromFile("C:\Users\Public\sImg1384.jpg")
PictureBox1.Image = Image.FromFile("C:\Users\Public\sImg1386.jpg")

The alternative is to paint the image onto the object.  This provides much more control over how the image is placed and sized.  The following code copies the image onto the form in a rectangle positioned on the form at (100,100) and with size (300,300).  The image is resized to fit.

Dim BMP As Bitmap

Private Sub Form1_Load(ByVal sender As System.Object, _
               ByVal e As System.EventArgs) Handles MyBase.Load
  BMP = Bitmap.FromFile("C:\Users\Public\sImg1386.jpg")
End Sub

Private Sub Form1_Paint(ByVal sender As Object, _
      ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
  e.Graphics.DrawImage(BMP, New Rectangle(100, 100, 300, 300))
End Sub

Other overloads of the DrawImage method allow you to select a portion of the image to be copied.  Between selecting the source rectangle and defining the destination rectangle, virtually any combination of trimming and resizing is possible.

Below is an example that takes the central portion of the source bitmap and copies it to the same rectangle – a zoom function.

Dim BMP As Bitmap

Private Sub Form1_Load(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles MyBase.Load
  BMP = Bitmap.FromFile("C:\Users\Public\Album 1 116.jpg")
End Sub

Private Sub Form1_Paint(ByVal sender As Object, _
   ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
  Dim Srce As New Rectangle(BMP.Width \ 4, BMP.Height \ 4, _
                          BMP.Width \ 2,   BMP.Height \ 2)
  Dim Dest As New Rectangle(100, 100, 300, 300)
  e.Graphics.DrawImage(BMP, Dest, Srce, GraphicsUnit.Pixel)
End Sub

Adjusting that code to create a selectable zoom window, based on a numeric up/down control that ranges from 10 (very small)  to about 300 (very large) would look like this:

Private Sub Form1_Load(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles MyBase.Load
  BMP = Bitmap.FromFile("C:\Users\Public\Album 1 116.jpg")
End Sub

Private Sub Form1_Paint(ByVal sender As Object, _
   ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
  Dim Srce As Rectangle = New Rectangle(0, 0, BMP.Width, BMP.Height)
  Dim Dest As Rectangle = New Rectangle(100, 100, 300, 300)
  Dim Z As Integer = CInt(NumericUpDown1.Value)
  If Z <> 100 Then
    Dim ScaleSize As Double = 100 / Z
    Dim ScalePosn As Double = (1 - ScaleSize) / 2
    Srce = New Rectangle(BMP.Width * ScalePosn, BMP.Height * ScalePosn, _
                         BMP.Width * ScaleSize, BMP.Height * ScaleSize)
  End If
  e.Graphics.DrawImage(BMP, Dest, Srce, GraphicsUnit.Pixel)
End Sub

Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
  Me.Invalidate()
End Sub

Continued…

  1. Leave a comment

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