2013-08-08 2 views
1

조금 n00b 질문 나는 여전히 많은 StackOverflow 답변을 읽고 이해하지 않습니다.WriteableBitmap 작업의 WritePixels

변수는 Kinect에서 25 번 업데이트 된 바이트 배열입니다. UI 구성 요소가 없습니다.

WriteableBitmap 및 WritePixels가 동일한 작업의 스레드에서 호출 된 것으로 생각했습니다. 하지만 여전히 System.InvalidOperationException이 발생합니다. 각 루프에 대해 새로운 WriteableBitmap을 만들면 오류가 없습니다.

WriteableBitmap을 효율적으로 재사용하려면 어떻게해야합니까?

private async Task DoJob(TimeSpan dueTime, TimeSpan interval, CancellationToken token) { 

    if (dueTime > TimeSpan.Zero) 
    await Task.Delay(dueTime, token); 

    WriteableBitmap bitmap = NewColorBitmap(colorW, colorH); 

    // Repeat this loop until cancelled. 
    while (!token.IsCancellationRequested) { 

    try { 
     bitmap.WritePixels(
     new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), 
     colorData, bitmap.PixelWidth * Bgra32BytesPerPixel, 0); 
    } 
    catch(Exception ex){ 
     // System.InvalidOperationException: 
     // The calling thread cannot access this object 
     // because a different thread owns it. 
    } 

    // Wait to repeat again. 
    if (interval > TimeSpan.Zero) 
     await Task.Delay(interval, token); 
    } 
} 
+0

이 코드가 병렬입니까? –

답변

0

WriteableBitmap은 WPF 렌더링 스레드에 바인딩되어 있으므로 프로세스 간 통신을 위해 복잡한 코드를 작성해야합니다.

그래서 더 이상 사용하지 않고 더 나은 성능을 가진 Emgu CV (Open CV)의 이미지를 사용합니다.

0

Calling the WriteableBitmap.WritePixels method

체크 높이와 폭의 값. 아마도 바이트 배열은 충분히 크지 않을 것입니다! 그리고 stride는 메모리에있는 픽셀의 한 행에서부터 메모리에있는 픽셀의 다음 행까지의 바이트 수입니다.

+0

주요 문제는 WriteableBitmap이 ThreadSafe가 아니라는 것입니다. –

관련 문제