user85511
user85511

Reputation: 533

clearing the image in a picturebox

I have a form with a picturebox that will allow to draw a free hand picture.

I added the initialization of the image in the form_load and the click event of the clear button. When I click the clear button the image is cleared and at the mouse move on the picturebox the last drawned image will shown.

My question is, how can I validate the picturebox whether it is empty or not? Just that I don't want to permit to save an empty image.

Upvotes: 8

Views: 115285

Answers (6)

flip
flip

Reputation: 1

I just drew a filled in blank rectangle to cover the image in the beginning of the code

Dim graph As Graphics = pbChart.CreateGraphics
               'to clear image
        graph.FillRectangle(Brushes.Azure, 0, 0, 500, 500)

Upvotes: -1

ali
ali

Reputation: 29

if you need to reset your image in picturebox you can do this code (in c#)

  private void  your_name_picturebox_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        your_name_picturebox.Image=null;
     }

Upvotes: 1

Peter
Peter

Reputation: 38465

C# bool clearImage; clearButton_Click(...) { clearImage = true; img.Invalidate(); }

img_Paint(...)
{
  if (clearImage)
  {
    clearImage = false;
    e.Graphics.Clear(Color.White);
  }
}

VB.NET

Private clearImage As Boolean 
Private Sub button_click(ByVal sender As Object, ByVal e As eventargs) Handels....
    clearImage = True
    img.Invalidate()
End Sub
Private Sub img_Paint(ByVal sender As Object, ByVal e As ...) Handels ....
    If clearImage Then
        clearImage = False
        e.Graphics.Clear(Color.White)
    End If
End Sub

Upvotes: 1

Crouchie
Crouchie

Reputation: 71

Private Sub ClearPictureBox(pb As PictureBox)
    pb.Image = Nothing
    pb.BackColor = Color.Empty
    pb.Invalidate()
End Sub

Upvotes: 7

xpda
xpda

Reputation: 15813

There might be some logic error in your code. Check the mousemove or mousehover event and make sure it is not assigning an image to the picturebox.

Instead of assigning nothing to the new image with the button, you could clear the the existing image. This might be better because you won't have to create a new image before you can draw on it again.

g = Graphics.FromImage(PictureBox1.Image)
g.Clear(PictureBox1.BackColor)

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94645

PictureBox1.Image = Nothing

Upvotes: 14

Related Questions