Search code examples
c#.netloadingsplash-screen

C# .NET if image is loaded


I need to find out if my pictureboxes have loaded images. I create them and give them ImageLocation. Pictures DO load after some time but I need to check if it is loaded. I tried using

if(pb.ImageLocation != null){
Console.WriteLine("Loaded!");
}

But this shows that its loaded even if it actually isn't. Also I have a bunch of dynamic created pictureboxes:

void CreateBrick(int x,int y)
    {
        bricks[i] = new PictureBox();
        bricks[i].Name = "pb_b" + i.ToString();
        bricks[i].Location = new Point(y, x);
        bricks[i].Size = new Size(60, 60);
        bricks[i].ImageLocation = @"Images/brick_wall.jpg";
        pb_bg.Controls.Add(bricks[i]);
        brick.Add(bricks[i]);
        i++;
    }

And I have no idea how to check these...


Solution

  • The problem in your code is that you don't call the Load or LoadAsync method.

    void CreateBrick(int x,int y)
    {
        bricks[i] = new PictureBox();
        bricks[i].Name = "pb_b" + i.ToString();
        bricks[i].Location = new Point(y, x);
        bricks[i].Size = new Size(60, 60);
        // You can pass the path directly to the Load method
        // bricks[i].ImageLocation = @"Images/brick_wall.jpg";
        bricks[i].Load(@"Images/brick_wall.jpg");
        pb_bg.Controls.Add(bricks[i]);
        brick.Add(bricks[i]);
        i++;
    }
    

    If you use Load method then then the image is loaded after the call, if you use LoadAsync you could add the event handler for the LoadComplete event.

    bricks[i].LoadCompleted += onLoadComplete;
    bricks[i].LoadAsync(@"Images/brick_wall.jpg");
    ....
    
    private void onLoadComplete(Object sender, AsyncCompletedEventArgs e)
    {
        // Don't forget to check if the image has been really loaded,
        // this event fires also in case of errors.
        if (e.Error == null && !e.Cancelled)
            Console.WriteLine("Image loaded");
        else if (e.Cancelled)
            Console.WriteLine("Load cancelled");
        else
            Console.WriteLine("Error:" + e.Error.Message);
    
    }
    

    If you want to use the LoadAsync approach you still have to solve the problem how to match the complete load of a particular image to the related picture box. This could be solved using the sender parameter of the LoadAsync. This sender parameter is the PictureBox who has completed the load of the image.
    You can use the Tag property and set it to "1" to mark your picturebox as loaded and to the error message in case of problems.

    private void onLoadComplete(Object sender, AsyncCompletedEventArgs e)
    {
        PictureBox pic = sender as PictureBox;
        // Don't forget to check if the image has been really loaded,
        // this event fires also in case of errors.
        if (e.Error == null && !e.Cancelled)
        {
            pic.Tag = "1";
            Console.WriteLine("Image loaded");
        }
        else
        {
            pic.Tag = e.Error.Message;
            Console.WriteLine("Cancelled:" + e.Error.Message);
        }
    }
    

    After this the pictureboxes in your bricks arrays have their Tag property marked as "1" for the loaded ones and with an error message for the ones with an error.