Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
TitleLet the user select an irregular area and overlay it on another picture in VB .NET
Keywordsregion, path, area, select, overlay, copy, VB.NET
CategoriesGraphics, VB.NET
 
This program uses a PictureBox's MouseDown, MouseMove, and MouseUp event handlers to let the user scribble to select an area. It saves the points drawn by the user in the m_Points array.

The MouseUp event handler adds a point to close the area if necessary. It then uses the points to create a graphics Path object. It makes a Bitmap containing a copy of a background picture and then makes a Graphics object attached to the Bitmap. It uses the Graphics object's SetClip method to restrict drawing to the Path and then uses DrawImage to copy the original picture onto the background (in the restricted area).

 
Private m_Points() As Point
Private m_MaxPoint As Integer

' Start selecting a region.
Private Sub picVisible_MouseDown(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    picVisible.MouseDown
    ' Erase any previous drawing.
    picVisible.Image = DirectCast(picOriginal.Image.Clone, _
        Bitmap)

    ' Save the starting point.
    m_MaxPoint = 0
    ReDim m_Points(m_MaxPoint)
    m_Points(m_MaxPoint).X = e.X
    m_Points(m_MaxPoint).Y = e.Y
End Sub

' Continue selecting a region.
Private Sub picVisible_MouseMove(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    picVisible.MouseMove
    ' Do nothing if we're not selecting a region.
    If m_Points Is Nothing Then Exit Sub

    ' Save the new point.
    m_MaxPoint += 1
    ReDim Preserve m_Points(m_MaxPoint)
    m_Points(m_MaxPoint).X = e.X
    m_Points(m_MaxPoint).Y = e.Y

    ' Draw the latest line.
    Dim gr As Graphics = picVisible.CreateGraphics
    gr.DrawLine(Pens.Yellow, _
        m_Points(m_MaxPoint).X, _
        m_Points(m_MaxPoint).Y, _
        m_Points(m_MaxPoint - 1).X, _
        m_Points(m_MaxPoint - 1).Y)
End Sub

' Finish selecting a region.
Private Sub picVisible_MouseUp(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    picVisible.MouseUp
    ' Do nothing if we're not selecting a region.
    If m_Points Is Nothing Then Exit Sub

    ' Close the region.
    If (m_Points(0).X <> m_Points(m_MaxPoint).X) Or _
       (m_Points(0).Y <> m_Points(m_MaxPoint).Y) _
    Then
        ' Save the new point.
        m_MaxPoint += 1
        ReDim Preserve m_Points(m_MaxPoint)
        m_Points(m_MaxPoint).X = m_Points(0).X
        m_Points(m_MaxPoint).Y = m_Points(0).Y
    End If

    ' Make the points into a Path.
    Dim selected_path As New _
        System.Drawing.Drawing2D.GraphicsPath(Drawing2D.FillMode.Winding)
    selected_path.AddLines(m_Points)

    ' Make the drawing permanent.
    Dim bm_visible As Bitmap = _
        DirectCast(picOriginal.Image.Clone, Bitmap)
    Dim gr_visible As Graphics = _
        Graphics.FromImage(bm_visible)
    gr_visible.DrawPath(Pens.Orange, selected_path)
    picVisible.Image = bm_visible

    ' Copy the picture onto picResult,
    ' restricting it to the selected region.
    Dim bm_result As Bitmap = _
        DirectCast(picBackground.Image.Clone, Bitmap)
    Dim gr_result As Graphics = _
        Graphics.FromImage(bm_result)
    ' Clip to the path.
    gr_result.SetClip(selected_path)
    ' Copy the image.
    gr_result.DrawImage(picOriginal.Image, 0, 0)
    ' Display the result.
    picResult.Image = bm_result

    ' We're no longer selecting a region.
    m_Points = Nothing
End Sub
 
For more information on graphics programming in Visual Basic (not VB .NET), see my book Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated