我有一个程序,Kinect获取图像并将其保存到用户指定的位置。我知道该程序找到了正确的文件夹,因为它创建了更多的文件夹来保存不同类型的图像,并且这些文件夹将被创建。我目前用于保存图像的代码(下面)适用于其他程序,那么是否有一些参数正在阻止它,我不知道?提前致谢。为什么我的程序不保存图像?
保存图象
using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
{
if (colorFrame == null)
{
return;
}
byte[] pixels = new byte[sensor.ColorStream.FramePixelDataLength];
//WriteableBitmap image = new WriteableBitmap(
// sensor.ColorStream.FrameWidth,
// sensor.ColorStream.FrameHeight, 96, 96,
// PixelFormats.Bgra32, null);
colorFrame.CopyPixelDataTo(pixels);
colorImage.WritePixels(new Int32Rect(0, 0, colorImage.PixelWidth,
colorImage.PixelHeight),
pixels, colorImage.PixelWidth * 4, 0);
//BitmapSource image = BitmapSource.Create(colorFrame.Width, colorFrame.Height,
// 96, 96, PixelFormats.Bgr32, null,
// pixels, colorFrame.Width * 4);
//image.WritePixels(new Int32Rect(0, 0, image.PixelWidth, image.PixelHeight),
// pixels, image.PixelWidth * sizeof(int), 0);
//video.Source = image;
totalFrames++;
BitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(colorImage));
//path = System.IO.Path.Combine("C:/", "Kinected", "Images");
if (PersonDetected == true)
{
if (totalFrames % 10 == 0)
{
if (file_name != null && colorImage != null)
{
try
{
using (FileStream fs = new FileStream(colorPath +
@"\Kinected Image " + time + ".jpg", FileMode.Create))
{
encoder.Save(fs);
}
}
catch (IOException)
{
System.Windows.MessageBox.Show("Save Failed");
}
}
}
skeletonDeLbl.Content = "Skeleton Detected!";
}
if (PersonDetected == false) skeletonDeLbl.Content = "No Skeleton Detected.";
}
确定路径
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description =
"Select which folder you want Kinected to keep all of its information/images in.";
DialogResult result = dialog.ShowDialog();
colorPath = dialog.SelectedPath + @"\Color Images";
depthPath = dialog.SelectedPath + @"\Depth Images";
facePath = dialog.SelectedPath + @"\Face Data";
if (!Directory.Exists(colorPath))
Directory.CreateDirectory(colorPath);
if (!Directory.Exists(depthPath))
Directory.CreateDirectory(depthPath);
if (!Directory.Exists(facePath))
Directory.CreateDirectory(facePath);
System.Windows.MessageBox.Show(colorPath);
编辑
原来file_name
只是空的,但现在我得到的错误时它到达了行using (FileStream fs = new FilesStream(file_name, FileMode.Create))
它说:
An unhandled exception of type 'System.NotSupportedException' occurred in mscorlib.dll
Additional information: The given path's format is not supported.
为什么会发生这种情况?我使用的是与微软演示完全相同的代码,并且在那里可以正常工作。谢谢。
“不保存”是什么意思?你有错误吗?在最后没有任何文件,但它似乎工作正常?当你期待派,你会得到蛋糕吗? –
是否满足条件'PersonDetected && totalFrames%10 == 0 && file_name!= null && colorImage!= null'? I.e是完全执行的保存代码?使用调试器来追踪问题。 –
@lc。没有文件末尾 – Kinected