如果加载了映像,则使用C#.NET
本文关键字:NET 加载 映像 如果 | 更新日期: 2023-09-27 18:05:02
我需要了解我的picturebox是否加载了图像。我创建它们并给它们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++;
}
我不知道如何检查这些。。。
代码中的问题是没有调用Load或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
您可以使用Tag属性并将其设置为"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);
}
}
在此之后,砖块阵列中的picturebox的Tag属性对于加载的图片框标记为"1",对于出现错误的图片框则标记为错误消息。
在按照此处描述的方式分配ImageLocation后,是否可以尝试使用PictureBox的LoadCompleted
事件
当然,您可以确保图像是异步加载的。
pb.WaitOnLoad = false;
然后异步加载图像:
pb.LoadAsync("some.gif");
有关stackoverflow的更多信息,您可以在这里和这里查看
分配如下事件处理程序:
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" );
}
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;
}
}
可以检查其返回
bool isUploded = CheckUplodedImage();
if(isUploded)
{
'' ...uploaded
'' Perform Operation
}
else
'' not uploaded