Dear Sir,

How to detect picture has a picture on empty?

Suppose , I droped a picturebox control on form, there is no image loaded in it, what codes should I needed those will tell me that picturebox is empty?

Please help

Dani AI

Generated

Short answer: is right — the PictureBox stores its image in the Image property, so checking that property is the basic test. The simple check works in the vast majority of cases, but there are a few practical gotchas and more robust patterns worth using.

Common pitfalls and safer checks

  • A design-time placeholder or a resource image may mean Image is already set even though you consider the box "empty." If you need to know whether the user actually loaded an image, store the source (path/URL) or a boolean in PictureBox.Tag when you load it.
  • Some images can be tiny (1x1 transparent) or corrupted; verifying Image.Width/Image.Height > 1 is a quick extra filter. Wrap such checks in a try/catch to avoid GDI+ exceptions on disposed images.
  • When loading from disk, clone the image to avoid locking the file and to ensure you can safely dispose/reload later:
Using fs As IO.FileStream = IO.File.OpenRead(path)
    Dim src = Image.FromStream(fs)
    PictureBox1.Image = New Bitmap(src)   'clone so file can be closed
End Using
PictureBox1.Tag = path    'track source if needed

A small helper

Public Function PictureBoxHasContent(pb As PictureBox) As Boolean
    If pb Is Nothing Then Return False
    Dim img = pb.Image
    If img Is Nothing Then Return False
    Try
        Return img.Width > 1 AndAlso img.Height > 1
    Catch
        Return False
    End Try
End Function

Notes for ASP.NET pages

  • For WebForms use the Image.ImageUrl (or FileUpload.HasFile when uploading). An empty or default placeholder URL means "no image" from the app's perspective.

Final tips

  • Free old images (Dispose) before assigning new ones to avoid leaks.
  • If checking from a background thread, marshal to the UI thread (Invoke) before touching the control.

Check the Image property of PictureBox as Nothing ..
Ex:

If PictureBox1.Image Is Nothing Then
            MsgBox("No Picture")
        Else
            MsgBox("Some Picture is there")
        End If
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.