2

我有一个保存图像的程序,但是当我调试它时,我得到了这个错误:

不支持给定路径的格式。

我想知道为什么不支持它,以及如何解决这个问题。提前致谢

我的代码

BitmapSource image = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 
                     96, 96, PixelFormats.Bgr32, null, pixels, stride);

ImageFormat format = ImageFormat.Jpeg;

string file_name = "C:\\Kinected\\Images\\Kinect" + bb1 + ".jpg";

image.Save(file_name, format);

编辑

我已经添加了代码,它可以正确编译,但文件永远不会保存。这是代码:

string mypath = System.IO.Path.Combine(@"C:\", "Kinected", "Images");
if (!Directory.Exists(mypath))
  {
       Directory.CreateDirectory(mypath);
       file_name = System.IO.Path.Combine(mypath, "Kinect 1" + bb1 + ".jpeg");
  }

if (file_name == null)
 {
       return;
 }

if (!Directory.Exists(file_name))
 {
        Directory.CreateDirectory(file_name);
 }

编辑

我已经添加了以下所有代码,但仍然The given path's format is not supported.出现错误。再次感谢。

BitmapSource image = BitmapSource.Create(
                colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride);

            totalFrames = colorFrame.FrameNumber;
            ImageFormat format = ImageFormat.Jpeg;                

            if (PersonDetected == true)
            {
                if (!Directory.Exists(mypath))
                {
                    Directory.CreateDirectory(mypath);
                    file_name = "C:\\Kinected\\Images\\Kinect 1 " + bb1 + ".jpeg";
                }
                if (file_name == null || mypath == null || image == null)
                {
                    if (mypath == null)
                    {
                        mypath = System.IO.Path.Combine("D:/", "Kinected", "Images");

                        if (!Directory.Exists(mypath))
                        {
                            Directory.CreateDirectory(mypath);
                        }
                    }

                    if (file_name == null)
                    {
                        file_name = "D:\\Kinected\\Images\\Kinect " + bb1 + ".jpeg";
                    }

                    if (image == null)
                    {
                        image = BitmapSource.Create(
                                     colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride);
                    }
                }

                if (totalFrames % 10 == 0)
                {
                    if (file_name != null && image != null && format != null)
                    {
                        image.Save(file_name, format);//where I get the error
                    }
                }
            }
4

2 回答 2

2

总是让路径像

string mypath = Path.Combine(@"C:\", "Kinected", "Images");

产生这个问题的地方很多。

1. 确保创建路径。

if(!Directory.Exists(mypath))
    Directory.CreateDirectory(mypath);

2. 可能目录已经创建,但是你没有C:的权限。将 C: 更改为 D:

string file_name = Path.Combine(@"D:\", "Kinect" + bb1 + ".jpg");

现在检查它是否在那里创建。

阅读有关此保存方法的更多信息。

于 2012-05-07T02:17:56.583 回答
-1

尝试使用文件名:

string file_name = "c:\\example\\path\\afile.jpg";
于 2012-05-07T02:16:12.333 回答