gpt4 book ai didi

c# - 如何检测图像何时出现在 PictureBox 中

转载 作者:行者123 更新时间:2023-12-04 10:42:37 25 4
gpt4 key购买 nike

我对 System.Windows.Forms.PictureBox 有疑问。我想在监视器上显示图像并在相机上捕获它。所以我使用包含图片框的 Winform。图片框是:

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。 Image set 方法似乎是一种异步方法。有什么方法可以确认图像已设置吗?谢谢。

最佳答案

我会使用第一个 Paint分配新的 Image 后的事件.
您可以尝试使用非常大的 image url 来自 this site .

该示例使用谷歌 Logo 图片网址。复制以下代码并确保将事件处理程序分配给事件:

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

关于c# - 如何检测图像何时出现在 PictureBox 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59855806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com