我使用kinect v2流来显示彩色图像和身体图像。 现在我想保存彩色图像,但如果我在主线程中添加代码,我不会显示任何内容,因为在硬盘上每秒写入30个文件。 所以我必须想使用线程,特别是线程池,但我有框架例外:C#使用ThreadPool将文件写入硬盘的线程
ColorCameraSettings 'colorFrame.ColorCameraSettings' ha generato un'eccezione di tipo 'System.ObjectDisposedException' Microsoft.Kinect.ColorCameraSettings {System.ObjectDisposedException}
这是我的代码:
private void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
{
var reference = e.FrameReference.AcquireFrame();
this.StatusText = "NO BODY";
frameCount++;
//Console.WriteLine(frameCount);
bool dataReceived = false;
//sw.WriteLine("frame nuovo");
using (ColorFrame frame = reference.ColorFrameReference.AcquireFrame())
{
if (frame != null)
{
//image.Source = ToBitmap(frame);
ThreadPool.QueueUserWorkItem(writeColorImage, frame);
}
}..............
,并写:
private void writeColorImage(Object frame)
{
ColorFrame colorFrame = (ColorFrame)frame;
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(ToBitmap(colorFrame)));
using (var filestream = new FileStream(filelocation + "image" + frameCount + ".jpg", FileMode.Create))
encoder.Save(filestream);
}
private BitmapSource ToBitmap(ColorFrame frame)
{
int width = frame.FrameDescription.Width;
int height = frame.FrameDescription.Height;
PixelFormat format = PixelFormats.Bgr32;
byte[] pixels = new byte[width * height * ((PixelFormats.Bgr32.BitsPerPixel + 7)/8)];
if (frame.RawColorImageFormat == ColorImageFormat.Bgra)
{
frame.CopyRawFrameDataToArray(pixels);
}
else
{
frame.CopyConvertedFrameDataToArray(pixels, ColorImageFormat.Bgra);
}
int stride = width * format.BitsPerPixel/8;
return BitmapSource.Create(width, height, 96, 96, format, null, pixels, stride);
}
如何可以在硬盘上写这张图片吗?谢谢
错误,对象不具备这种方法 – luca 2014-10-30 14:08:00
@luca对不起,本来应该colorFrame,我编辑它。 – 2014-10-30 14:09:41
好吧,但原来的框架?不处理? – luca 2014-10-30 14:11:38