So I'm trying to programe a kinect sensor with the SDK. Here is the code.
public partial class MainWindow : Window
{
bool mirror=false;
bool displayActive = true;
int redOffset;
int greenOffset;
int blueOffset;
WriteableBitmap colorImageBitmap = null;
KinectSensor myKinect;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
kinectVideo.Source = colorImageBitmap;
myKinect = KinectSensor.KinectSensors[0];
if (KinectSensor.KinectSensors.Count == 0)
{
MessageBox.Show("No Kinects detected", "Camera Viewer");
Application.Current.Shutdown();
}
myKinect.ColorStream.Enable();
myKinect.Start();
Thread updateVideoThread;
updateVideoThread = new Thread(new ThreadStart(videoDisplay));
updateVideoThread.Start();
}
void videoDisplay()
{
while (displayActive)
{
using (ColorImageFrame colorFrame = myKinect.ColorStream.OpenNextFrame(10))
{
if (colorFrame == null) continue;
byte[] colorData = new byte[colorFrame.PixelDataLength];
colorFrame.CopyPixelDataTo(colorData);
//----------------------------Methodos 2 for image color adjustment------------------------------------------------------------------------------------
updateColor(colorData);
//----------------------------Methodos 1 for mirror------------------------------------------------------------------------------------
if (mirror) { reflectImage(colorData, colorFrame.Width, colorFrame.Height); }
//----------------------------Methodos 2 for update image------------------------------------------------------------------------------------
kinectVideo.Source = colorImageBitmap;
if (colorImageBitmap == null)
{
this.colorImageBitmap = new WriteableBitmap(colorFrame.Width,
colorFrame.Height,
96, // DpiX
96, // DpiY
PixelFormats.Bgr32,
null);
}
this.colorImageBitmap.WritePixels(new Int32Rect(0, 0, colorFrame.Width, colorFrame.Height),
colorData, // video data
colorFrame.Width * colorFrame.BytesPerPixel, // stride,
0 // offset into the array - start at 0
);
}
}
}
and in the line "kinectVideo.Source = colorImageBitmap;" is gives me an exception that says "The calling thread cannot access this object because a different thread owns it.". I dont understand why. I am new in programming with C# and Visual Studio. I have only one Thread so I don't know why the exception. Any help?