C#.NET如果加载了图像

时间:2016-08-31 12:03:00

标签: c# .net loading splash-screen

我需要查看我的图片框是否已加载图片。 我创建它们并给它们ImageLocation。图片在一段时间后加载,但我需要检查它是否已加载。我尝试使用

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

但这显示它即使实际上没有加载也是如此。我还有一堆动态创建的图片框:

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++;
    }

我不知道如何检查这些...

3 个答案:

答案 0 :(得分:1)

Could you try using LoadCompleted event of PictureBox after you assign a ImageLocation as the way it is described here

You can make sure images are loaded asynchronously of course.

pb.WaitOnLoad = false;

Then load the image asynchronously:

pb.LoadAsync("some.gif");

For more from stackoverflow you can have a look here and here

Assign an event handler like the following:

pb.LoadCompleted += PictureBox1_LoadCompleted; 

Sample event handler right from msdn:

private void PictureBox1_LoadCompleted(Object sender, AsyncCompletedEventArgs e) {
  System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
  messageBoxCS.AppendFormat("{0} = {1}", "Cancelled", e.Cancelled );
  messageBoxCS.AppendLine();
  messageBoxCS.AppendFormat("{0} = {1}", "Error", e.Error );
  messageBoxCS.AppendLine();
  messageBoxCS.AppendFormat("{0} = {1}", "UserState", e.UserState );
  messageBoxCS.AppendLine();
  MessageBox.Show(messageBoxCS.ToString(), "LoadCompleted Event" );
}

答案 1 :(得分:1)

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.

答案 2 :(得分:0)

private stattic bool CheckUplodedImage()
{
    bool return = false;
    try
    {
        PictureBox imageControl = new PictureBox();
        imageControl.Width = 60;
        imageControl.Height = 60;

        Bitmap image = new Bitmap("Images/brick_wall.jpg");
        imageControl.Image = (Image)image;

        Controls.Add(imageControl);
        return true;
   }
   catch(Exception ex)
   {
     return false;
   }
}

can check of its return

bool isUploded = CheckUplodedImage();
if(isUploded)
{
  \\ ...uploaded
  \\ Perform Operation
}
else
    \\ not uploaded