2012-07-28 50 views
0

這是我的相關代碼:幀在循環不更新

在窗體的負載方法:

Application.Idle += new EventHandler(delegate(object s, EventArgs ea) 
{ 
    if (!recording) 
    { 
     currentFrame = getFrame(); 
     ib.Image = currentFrame; 
    } 
    else 
    { 
     ib.Image = recFrame; 
    } 
    }); 

在按鈕按壓(IB是在表格上的ImageBox):

Measurements measurements = new Measurements(); 
String name = txtSampleName.Text; 
ib.Image = recFrame; 
stopwatch.Start(); 
recording = true; 
while ((count = (int)Math.Floor(stopwatch.ElapsedMilliseconds/(double)1000)) < finalTime) 
{ 
    currentFrame = getFrame();   
    Measurement currentMeasurement = new Measurement(name, currentFrame); 
    measurements.add(currentMeasurement); 
} 
recording = false; 
stopwatch.Stop(); 
measurements.write(txtDirectory.Text); 

問題是該框架實際上並未更新。 currentFrame採用循環開始前幀的值。 recFrame在ib.Image中不可見 - ib只是在循環的持續時間內掛起 - 並且不應該像填充recFrame那樣)。 currentMeasurement應該是添加到測量列表中,但其中的幀不是相機的當前幀。下面是從getFrame()方法,這是不是沒有在當前時間返回的是從攝像頭的代碼的一個總結,或者是和設置currentFrame正在其舊值,而不管:

private Image<Bgr, Byte> getFrame() 
{ 
    // get the frame 
    Image<Bgr, Byte> frame = capture.QueryFrame(); 

    // draw some stuff on top of the frame 
    // ... 

    // return 
    return frame; 
} 

我需要補充一些有點遲遲地等待一些東西來完成?我試過把Thread.Sleep放進去,它並沒有任何幫助。我不明白這個問題是什麼,所以一直沒有找到明智的解決方案。當我稍後將度量中的幀導出到AVI文件時,在循環開始之前所有幀都具有currentFrame的值,並且不會更改。

任何想法?

非常感謝,

Froskoy。

編輯:使用currentFrame.Copy()在線currentMeasurement =新的測量(名稱,currentFrame)意味着當前捕獲的幀實際上被放入列表中,然後寫入AVI文件。我不明白爲什麼?另外,recFrame仍然不在ib中顯示,所以我不明白這裏發生了什麼?

回答

1

嘗試在你的while循環的DoEvents呼叫

while ((count = (int)Math.Floor(stopwatch.ElapsedMilliseconds/(double)1000)) < finalTime) 
{ 
    currentFrame = getFrame(); 
    ib.Image = currentFrame; 
Application.DoEvents();    
    Measurement currentMeasurement = new Measurement(name, currentFrame); 
    measurements.add(currentMeasurement); 
} 
+0

謝謝!這就是訣竅! – Froskoy 2012-07-28 19:46:44

+0

您好,很高興爲您效勞 – 2012-07-29 02:32:30

+0

嗨,最好的辦法是使用Backgroundworker,如果您在Windows窗體應用程序下更新您的GUI ;-) – 2012-12-23 19:26:18