I am working on a GDI+ application in VB 2005 and can’t seem to find a solution for a problem with graphicspaths. This is a Windows application and I am allowing the user to freehand draw on a picturebox which has a background image loaded. I want to allow the user to erase any portion of the lines that have been drawn with an eraser tool. However, I don’t want the background image to appear erased. Is there a way to limit the eraser tool to the graphicspath. The only thing that comes to mind is some trick with regions, but, I’m not quite sure how to implement. Any graphics gurus out there who might be able to help? I’d be truly grateful. See code below:

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then 
            If FractureFlag Then
                mousePath.StartFigure()  
            ElseIf EraserFlag Then
                mousepath2.StartFigure()  
            End If
        End If
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            If FractureFlag Then
                Try
                    mousePath.AddLine(e.X, e.Y + 21, e.X, e.Y + 21)   
                Catch
                    MsgBox("No way, Hose!")
                End Try
            ElseIf EraserFlag Then
                Try
                    mousepath2.AddLine(e.X + 15, e.Y + 15, e.X + 15, e.Y + 15)    
                Catch
                    MsgBox("No way, Hose!")
                End Try
            End If

        End If

    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        g.PixelOffsetMode = PixelOffsetMode.HighQuality
        g.SmoothingMode = SmoothingMode.HighQuality
        g.CompositingQuality = CompositingQuality.HighQuality
        g.InterpolationMode = InterpolationMode.HighQualityBicubic

        If FractureFlag Then
            Try 
                myPenWidth = 2
                myUserColor = (Color.Black) 
                myAlpha = 100  
                Dim CurrentPen As New _
                      Pen(Color.FromArgb(myAlpha, myUserColor), myPenWidth) 
                e.Graphics.DrawPath(CurrentPen, mousePath) 
                g.DrawPath(CurrentPen, mousePath) 
            Catch
                MsgBox("Not happening!")
            End Try
        ElseIf EraserFlag Then
            Try 
                myUserColor = (Color.White) 
                myAlpha = 255  
                myPenWidth = 15
                Dim CurrentPen As New _
                     Pen(Color.FromArgb(myAlpha, myUserColor), myPenWidth) 
                e.Graphics.DrawPath(CurrentPen, mousepath2)  
                g2.DrawPath(CurrentPen, mousepath2)  
            Catch
                MsgBox("Not happening too!")
            End Try
        End If
        g3.DrawImage(bm, 0, 0)
        g3.DrawImage(bm2, 0, 0)
        e.Graphics.DrawImage(bm3, New Point(0, 0))
    End Sub

I'm working on a graphics editor in VB 2005 as well, just for the heck of it and to see how far can I go and I haven't tackled the problem you are having yet, but I know it is coming.

Two solutions come to mind. First, if you are drawing onto a transparent image on top of the background then you would have no problem removing the alterations. This would require the creation of some sort of layering system. I know I will need it, but I haven't thought out how to implement it yet.

The second solution might be a lot easier. Every time the user selects a new tool (I am assuming there is more than one tool for drawing) you clone the image and save it for use when erasing anything done with the tool. You would need to lock both bitmaps into memory and marshal bits between them based on the mouse coordinates of the eraser icon. Something like this would have to be done since you can't erase something written on top of an image without saving the underlying image first.

Sorry I can't be of more help, but I'll be following this thread with bated breath since I have to do the exact thing sometime soon.

Solution two seems reasonable. If I place the background image on the form and make the picturebox tranparent, then I may be able to restore the background once the eraser has finished. However, It seems that the eraser will still appear to erase the background until the action is done. Perhaps, i need to think about transparency and the pen.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.