0

I'm using VB.NET to load images from a URL into a PictureBox control using LoadAsync. These are often very small images - sometimes 76x76 pixels. The delay in displaying them onscreen is extreme. I initially thought it was due to load time, but I added a log to the LoadComplete event and found that the image took 0.29 seconds to load but an additional 21 seconds (per my stopwatch) to display.

Public Sub ShowImage(ByRef image As PictureBox, imgUrl as String)
    Log("Starting to load image")
    AddHandler image.LoadCompleted, ImgFinish(image)
    image.Image = Nothing
    image.CancelAsync()
    image.LoadAsync(imgUrl)
    image.Visible = True

End Sub
Private Function ImgFinish(ByRef image As PictureBox)
    Log("Image loading complete")
    Return Nothing
End Function

And the log output:

(17:03:59.930) vb_Utilities.ShowImage[697] - V:Starting to load image
(17:04:00.220) vb_Utilities.ImgFinish[731] - Image loading complete

Then using a stopwatch I timed 21 additional seconds - to 17:25 - before the image appeared)

I also tried adding image.Refresh() to the function ImgFinish thinking that perhaps it just wasn't redrawing, but that didn't help.

What could be causing this delay? What can I check or try?

Scott
  • 3,663
  • 8
  • 33
  • 56
  • 1
    No idea on the problem, but you should change the signature of `ImgFinish` to match the event. See: [PictureBox.LoadCompleted Event](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.picturebox.loadcompleted?view=net-5.0#examples). – TnTinMn Nov 21 '20 at 01:17
  • changing the handler to "Addressof ImgFInish" stacks handlers - that is every time it's called it re-adds the handler for the event so eventually ImgFinish is called 100 times or more. For a reason that I don't really know, calling the function this way executes it only once – Scott Nov 21 '20 at 21:02
  • "it re-adds the handler for the event so eventually ImgFinish is called 100 times or more....calling the function this way executes it only once" - Solving a problem caused by a poor coding decision with some hack is not a good idea. Only add the handler once in either the constructor or OnLoad method. Alternatively, add it as you are currently doing, but remove it in `ImgFinish`. – TnTinMn Nov 21 '20 at 21:19
  • I literally only added it to record the timestamp for LoadComplete. It's not part of my solution and I'm deleting it once I figure out why the picture appears 21 seconds after it's loaded – Scott Nov 21 '20 at 21:38

0 Answers0