繁体   English   中英

为什么我的程序不保存图像?

[英]Why is my program not saving images?

我有一个程序,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.

为什么会这样呢? 我使用的代码与Microsoft演示的代码完全相同,并且在这里可以正常工作。 谢谢。

您应该使用以下代码将字符串组合到路径中

colorPath = System.IO.Path.Combine(dialog.SelectedPath, "Color Images");

Combine方法负责在必要时添加或删除反斜杠。


并且不要忘记使用调试器。 您可以设置断点并检查变量并执行更多操作。

调试器是您最好的朋友!


UPDATE

您还在文件名中使用了无效字符。 此方法替换无效字符,并对文件名也应用其他一些修复程序

/// <summary>
///   Replaces invalid characters in a file name by " ". Apply only to the filename.ext
///   part, not to the path part.
/// </summary>
/// <param name="fileName">A file name (not containing the path part) possibly
///   containing invalid characters.</param>
/// <returns>A valid file name.</returns>
public static string GetValidFileName(string fileName)
{
    string invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars()));
    string s = Regex.Replace(fileName, "[" + invalidChars + "]", " ");
    s = Regex.Replace(s, @"\s\s+", " "); // Replace multiple spaces by one space.
    string fil = Path.GetFileNameWithoutExtension(s).Trim().Trim('.');
    string ext = Path.GetExtension(s).Trim().Trim('.');
    if (ext != "") {
        fil += "." + ext;
    }
    fil = fil.Replace(" .", ".");
    return fil == "." ? "" : fil;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM