locked
Simple Lasso like free form selection tool RRS feed

  • Question

  • Just need to be able to Draw with the mouse the area that needs to be cut out or modified. Basically like the Photoshop's Lasso tool.
    Heres an example below.
    1

    Moving the selected.
    2
    Friday, October 2, 2009 2:13 AM

Answers

  • Start a new Windows Forms appliction.  Add a Panel and Dock.Fill it.  Add a PictureBox with SizeMode.AutoSize.  Replace the Form1 code with:

    Imports System.Drawing.Drawing2D
    Public Class Form1
      Dim LastPt As Point
      Dim GP As New GraphicsPath
      Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
        LastPt = e.Location
        GP.Reset()
        PictureBox1.Refresh()
      End Sub
      Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button = MouseButtons.Left Then
          GP.AddLine(LastPt, e.Location)
          PictureBox1.Invalidate(Rectangle.Round(GP.GetBounds))
          LastPt = e.Location
        End If
      End Sub
      Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        GP.CloseFigure()
        PictureBox1.Refresh()
        Using Frm2 As New Form1
          Using G As Graphics = Graphics.FromImage(Frm2.PictureBox1.Image)
            G.Clear(Frm2.PictureBox1.BackColor)
            G.FillPath(New TextureBrush(Me.PictureBox1.Image), GP)
          End Using
          Frm2.ShowDialog()
        End Using
      End Sub
      Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
        e.Graphics.DrawPath(Pens.Black, GP)
      End Sub
    End Class
    
    • Marked as answer by Zakukashi Sunday, October 4, 2009 4:32 AM
    Friday, October 2, 2009 8:23 AM

All replies

  • Can you show us the program that you have made for the surfact that is needed to use that: in your eyes "simple lasso", in my idea is the common bitmap not suitable for that and I'm curious how you made that surface?


    Success
    Cor
    Friday, October 2, 2009 3:58 AM
  • There is no program at the moment i just needed a general Idea on how to perform that function so that I can start it. Just a basic image editing program that can perform a snipping/lasso/Free selection which is like selecting a portion of a bitmap via mouse and modifying the area that was "drawn" to be selected. Just like in my first screenshot I drew the area to select(which wasn't so accurate since i had to draw again) and then I grabbed it with the mouse like i would to any rectangular selected methods.
    Friday, October 2, 2009 5:20 AM
  • Start a new Windows Forms appliction.  Add a Panel and Dock.Fill it.  Add a PictureBox with SizeMode.AutoSize.  Replace the Form1 code with:

    Imports System.Drawing.Drawing2D
    Public Class Form1
      Dim LastPt As Point
      Dim GP As New GraphicsPath
      Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
        LastPt = e.Location
        GP.Reset()
        PictureBox1.Refresh()
      End Sub
      Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button = MouseButtons.Left Then
          GP.AddLine(LastPt, e.Location)
          PictureBox1.Invalidate(Rectangle.Round(GP.GetBounds))
          LastPt = e.Location
        End If
      End Sub
      Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        GP.CloseFigure()
        PictureBox1.Refresh()
        Using Frm2 As New Form1
          Using G As Graphics = Graphics.FromImage(Frm2.PictureBox1.Image)
            G.Clear(Frm2.PictureBox1.BackColor)
            G.FillPath(New TextureBrush(Me.PictureBox1.Image), GP)
          End Using
          Frm2.ShowDialog()
        End Using
      End Sub
      Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
        e.Graphics.DrawPath(Pens.Black, GP)
      End Sub
    End Class
    
    • Marked as answer by Zakukashi Sunday, October 4, 2009 4:32 AM
    Friday, October 2, 2009 8:23 AM
  • THANKS!!!!!!!!!!!!!!
    Sunday, October 4, 2009 4:32 AM