What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake a map that shows a closeup of the part under the mouse in Visual Basic 2005
DescriptionThis example shows how to make a map that shows a closeup of the part under the mouse in Visual Basic 2005.
Keywordscloseup map, map, enlarge, closeup, close up, Visual Basic 2005
The program stores an enlarged version of the map in the hidden PictureBox picHidden. When the mouse moves, the program copies part of that image over the smaller image that is visible, giving a closeup of the area under the mouse.

When the program starts, it defines some constants. When the form loads, it saves references to the large and small (visible) images. It also makes a copy of the small image to modify later.

Private Const SCALE_FACTOR As Integer = 2
Private Const SMALL_RADIUS As Integer = 25
Private Const BIG_RADIUS As Integer = SMALL_RADIUS * _
Private Const BIG_DIAMETER As Integer = 2 * BIG_RADIUS

Private m_BigMap As Bitmap
Private m_OriginalWid As Integer
Private m_OriginalHgt As Integer
Private m_OriginalMap As Bitmap
Private m_ModifiedMap As Bitmap

Private m_SrcRect As New Rectangle(0, 0, BIG_DIAMETER, _
Private m_DestRect As New Rectangle(0, 0, BIG_DIAMETER, _

' Save the original small map image.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    m_OriginalWid = picMap.Image.Width
    m_OriginalHgt = picMap.Image.Height

    ' Save the big map.
    m_BigMap = picHidden.Image

    ' Save the original map.
    m_OriginalMap = picMap.Image

    ' Make a copy to display.
    m_ModifiedMap = m_OriginalMap.Clone()
End Sub
When the mouse moves, the program copies the original small map onto the modified version to erase the previous closeup. It adjusts the source and destination rectangles so they are centered over the area under the mouse and copies a piece of the big map onto the modified one. It draws a rectangle to outline the closeup and displays the result.
' Prepare the new map image.
Private Sub picMap_MouseMove(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.MouseEventArgs) Handles _
    Using gr As Graphics = Graphics.FromImage(m_ModifiedMap)
        ' Restore the original map.
        gr.DrawImage(m_OriginalMap, 0, 0, m_OriginalWid, _

        ' Copy a chunk of the big image into it.
        m_SrcRect.X = e.X * SCALE_FACTOR - BIG_RADIUS
        m_SrcRect.Y = e.Y * SCALE_FACTOR - BIG_RADIUS
        m_DestRect.X = e.X - BIG_RADIUS
        m_DestRect.Y = e.Y - BIG_RADIUS
        gr.DrawImage(m_BigMap, m_DestRect, m_SrcRect, _

        ' Outline the area.
        gr.DrawRectangle(Pens.Blue, m_DestRect)

        ' Display the result.
        picMap.Image = m_ModifiedMap
    End Using
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.