|
|
Title | Make a map that shows a circular closeup of the part under the mouse in Visual Basic 2005 |
Description | This example shows how to make a map that shows a circular closeup of the part under the mouse in Visual Basic 2005. |
Keywords | closeup map, map, enlarge, closeup, close up, circular region, Visual Basic 2005 |
Categories | Graphics |
|
|
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 * _
SCALE_FACTOR
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_Patch As Bitmap
Private m_PatchRect As New Rectangle(0, 0, BIG_DIAMETER, _
BIG_DIAMETER)
Private m_SrcRect As New Rectangle(0, 0, BIG_DIAMETER, _
BIG_DIAMETER)
Private m_DestRect As New Rectangle(0, 0, BIG_DIAMETER, _
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()
' Make a patch area.
m_Patch = New Bitmap(BIG_DIAMETER, BIG_DIAMETER)
End Sub
|
|
When the mouse moves, the program adjusts the source and destination rectangles for the big and small maps.
It then copies the small map image into the m_Patch bitmap. It fills an ellipse in the center of the patch and uses MakeTransparent to make this ellipse transparent.
Next the code copies the original small map onto the modified version to erase the previous closeup. It copies a piece of the big map onto the modified one.
The program then copies the patch over the enlarged area. Because the patch contains a transparent hole, this leaves the elliptical area in the middle enlarged but makes the corners look like the underlying small map.
Finally the program draws an ellipse outlining 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 _
picMap.MouseMove
' Adjust where the source and destination bitmaps are.
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
' Make a piece of the small map with a transparent hole
' in it.
Using gr As Graphics = Graphics.FromImage(m_Patch)
' Draw the small map image into the patch.
gr.DrawImage(m_OriginalMap, m_PatchRect, _
m_DestRect, GraphicsUnit.Pixel)
' Make a transparent hole in the patch.
Using br As New SolidBrush(Color.FromArgb(255, 1, _
2, 3))
gr.FillEllipse(br, m_PatchRect)
m_Patch.MakeTransparent(br.Color)
End Using
End Using
Using gr As Graphics = Graphics.FromImage(m_ModifiedMap)
' Restore the original map.
gr.DrawImage(m_OriginalMap, 0, 0, m_OriginalWid, _
m_OriginalHgt)
' Copy a chunk of the big image into it.
gr.DrawImage(m_BigMap, m_DestRect, m_SrcRect, _
GraphicsUnit.Pixel)
' Draw the patch to make the closeup round.
gr.DrawImage(m_Patch, m_DestRect, m_PatchRect, _
GraphicsUnit.Pixel)
' Outline the area.
gr.DrawEllipse(Pens.Blue, m_DestRect)
' Display the result.
picMap.Image = m_ModifiedMap
End Using
End Sub
|
|
|
|
|
|