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
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
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
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. Image.Width/Image.Height > 1 is a quick extra filter. Wrap such checks in a try/catch to avoid GDI+ exceptions on disposed images. 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
Image.ImageUrl (or FileUpload.HasFile when uploading). An empty or default placeholder URL means "no image" from the app's perspective.Final tips
Dispose) before assigning new ones to avoid leaks. 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 We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.