1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
private void toolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e){
int hr;
if (sampGrabber == null)
return;
if (savedArray == null)
{
int size = videoInfoHeader.BmiHeader.ImageSize;
if ((size < 1000) || (size > 16000000))
return;
savedArray = new byte[size + 64000];
}
pictureBox1.Image = null;
captured = false;
hr = sampGrabber.SetCallback(this, 1);
if (this.toolBarBtnGrab.Text == "Stopper l'analyse")
{
// Le thread est en route et on le tue
if(this.trouverLeCab.IsAlive){
this.trouverLeCab.Abort();
}
this.toolBarBtnGrab.Text = "Analyser";
}
else
{
// Le thread n'est pas en cours d'execution et on le lance
trouverLeCab = new Thread(new ThreadStart(this.trouverCab));
trouverLeCab.Start();
this.toolBarBtnGrab.Text = "Stopper l'analyse";
}
}
delegate void RecupererImage(string i);
public void recupererAfficherImage(string i)
{
try
{
int hr;
if (sampGrabber == null)
return;
hr = sampGrabber.SetCallback(null, 0);
int w = videoInfoHeader.BmiHeader.Width;
int h = videoInfoHeader.BmiHeader.Height;
if (((w & 0x03) != 0) || (w < 32) || (w > 4096) || (h < 32) || (h > 4096))
return;
int stride = w * 3;
GCHandle handle = GCHandle.Alloc(savedArray, GCHandleType.Pinned);
int scan0 = (int)handle.AddrOfPinnedObject();
scan0 += (h - 1) * stride;
Bitmap b = new Bitmap(w, h, -stride, PixelFormat.Format24bppRgb, (IntPtr)scan0);
handle.Free();
savedArray = null;
//b.Save("../../image/img" + i + ".bmp");
//b.Dispose();
//Image.Save("../../image/img"+i+".bmp", ImageFormat.Bmp);
//pictureBox1.Image = null;
//pictureBox1.Image = b;
//MessageBox.Show(this, "No video capture devices found!", "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
catch (Exception ee)
{
MessageBox.Show(this, "Could not grab picture\r\n" + ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
delegate void AjoutTextBoxText(string Texte);
public void AjoutTextBox(string Texte)
{
this.label1.Text = Texte;
}
private void trouverCab()
{
int i = 0;
// Tant que le thread n'est pas tué, on travaille
while (Thread.CurrentThread.IsAlive)
{
i = i + 1;
// Attente de 5000 ms
Thread.Sleep(5000);
// MessageBox.Show("");
//this.label1.Text = i.ToString();
AjoutTextBoxText ajoutText = new AjoutTextBoxText(AjoutTextBox);
Invoke(ajoutText, i.ToString());
RecupererImage test = new RecupererImage(recupererAfficherImage);
Invoke(test, i.ToString());
}
} |
Partager