C# 如何检测图像何时出现在PictureBox中
我对System.Windows.Forms.PictureBox有问题。我想在显示器上显示一幅图像,然后在相机上拍摄。所以我使用Winform,它包括一个picturebox。图片框为:C# 如何检测图像何时出现在PictureBox中,c#,.net,winforms,picturebox,C#,.net,Winforms,Picturebox,我对System.Windows.Forms.PictureBox有问题。我想在显示器上显示一幅图像,然后在相机上拍摄。所以我使用Winform,它包括一个picturebox。图片框为: PictureBox pb = new PictureBox(); pb.WaitOnLoad = true; 当我将位图设置为PictureBox并从相机捕获图像时 // Show bmp1 this.image.Image = bmp1; this.image.Invalidate(); this.im
PictureBox pb = new PictureBox();
pb.WaitOnLoad = true;
当我将位图设置为PictureBox并从相机捕获图像时
// Show bmp1
this.image.Image = bmp1;
this.image.Invalidate();
this.image.Refresh();
// Delay 1s
UseTimerToDelay1s();
// Show bmp2
this.image.Image = bmp2;
this.image.Invalidate();
this.image.Refresh();
// Capture
CaptureImageFromCamera();
它只捕获bmp1
如果我再加上一点这样的延迟
this.image.Image = bmp2;
this.image.Invalidate();
this.image.Refresh();
UseTimerToDelay100ms();
CaptureImageFromCamera();
它捕获bmp2。映像集方法似乎是一个异步方法。是否有任何方法确认图像已设置?谢谢。分配新的
图像后,我将使用第一个绘制
事件。
你可以用一个很大的from来试试
该示例使用google徽标图像url。复制以下代码并确保将事件处理程序分配给事件:
bool newImageInstalled = true;
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.WaitOnLoad = true;
}
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
if (!newImageInstalled)
{
newImageInstalled = true;
BeginInvoke(new Action(() =>
{
//Capturing the new image
using (var bm = new Bitmap(pictureBox1.ClientSize.Width,
pictureBox1.ClientSize.Height))
{
pictureBox1.DrawToBitmap(bm, pictureBox1.ClientRectangle);
var tempFile = System.IO.Path.GetTempFileName() + ".png";
bm.Save(tempFile, System.Drawing.Imaging.ImageFormat.Png);
System.Diagnostics.Process.Start(tempFile);
}
}));
}
}
private void button1_Click(object sender, EventArgs e)
{
newImageInstalled = false;
pictureBox1.ImageLocation =
"https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png";
}
private void button2_Click(object sender, EventArgs e)
{
newImageInstalled = false;
pictureBox1.Image = null;
}
那么,您想在设置图像后再次检查图像是否已设置?Notbool isSet=this.image.image==bmp2代码>?什么是UseTimerToDelay1s()
,UseTimerToDelay100ms()
?可能,使当前方法/处理程序异步和等待任务。延迟([毫秒间隔])
。使用计时器,您不会延迟任何事情,除非是计时器滴答声/经过事件本身设置了新的时间Bitmap@John:我只是编辑帖子以提供更多信息。所以我想要的是捕捉显示器上显示的图像。但图像似乎没有立即显示出来。我还尝试使用您的代码检查图像是否已设置。这是真的。但是图像仍然不能立即显示。@Jimi:我将计时器的间隔设置为延迟时间。将自动重置设置为false并已使用。谢谢。“油漆事件”是一个好主意,可以确认它是油漆的。最后我发现问题是由摄像头而不是程序引起的。谢谢大家,很抱歉,没问题。显然,超时将起作用,但post会尝试回答PictureBox中显示的检测图像的最快和特定可能时间。例如,像这样非常大的图像或更大的图像需要很长时间才能加载到PictureBox中。