C# .NET 如果图像被加载

C# .NET if image is loaded

我需要查明我的图片框是否加载了图片。 我创建它们并给它们 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++;
    }

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

按照描述的方式分配 ImageLocation 后,您能否尝试使用 PictureBox 的 LoadCompleted 事件 here

当然,您可以确保异步加载图像。

pb.WaitOnLoad = false;

然后异步加载图片:

pb.LoadAsync("some.gif");

更多来自Whosebug的内容你可以看看here and here

分配如下事件处理程序:

pb.LoadCompleted += PictureBox1_LoadCompleted; 

来自 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" );
}

您的代码中的问题是您没有调用 Load or LoadAsync 方法。

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

如果您使用 Load 方法,则在调用后加载图像,如果您使用 LoadAsync,则可以为 LoadComplete 事件添加事件处理程序。

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);

}

如果您想使用 LoadAsync 方法,您仍然必须解决如何将特定图像的完整加载与相关图片框相匹配的问题。这可以使用 LoadAsync 的 sender 参数来解决。这个sender参数就是图片加载完成的PictureBox
您可以使用标签 属性 并将其设置为“1”以将您的图片框标记为已加载并在出现问题时显示错误消息。

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

在此之后,您的积木阵列中的图片框将其标签 属性 标记为“1”(对于加载的图片),并为有错误的图片框标记错误消息。

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

可以查看其return

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