【问题标题】:Image's given path format not supported不支持图像的给定路径格式
【发布时间】:2012-05-15 14:20:56
【问题描述】:

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

不支持给定路径的格式。

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

我的代码

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
                    }
                }
            }

【问题讨论】:

标签: c# image image-processing format save


【解决方案1】:

总是让路径像

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");

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

阅读更多关于Save Method的信息。

【讨论】:

  • 大声笑我会把file_name改成实际的,检查编辑
  • 我收到了错误:The device is not ready Directory.CreateDirectory(mypath);
  • @OutlawLemur:为什么选择这条线? Directory.CreateDirectory(file_name); 此行不会创建目录。这是您的总文件路径,其中还包括文件名。 Directory.CreateDirectory 处理目录路径而不是文件路径。并尝试将 C: 的路径更改为 D:。可能是权利问题。
  • 我没有包含图像。保存,因为它与上次相比没有变化。另外,当我使用 D: 时,它会出现错误,我之前已经使用 C: 完成了此操作
  • 目录存在,我用过C:和D:,但是不支持格式,不是路径
【解决方案2】:

尝试使用文件名:

string file_name = "c:\\example\\path\\afile.jpg";

【讨论】:

  • 哈哈,我会把file_name 改成原来的样子,检查编辑
猜你喜欢
  • 2011-11-13
  • 2012-04-21
  • 1970-01-01
  • 2017-09-14
  • 2014-12-08
  • 1970-01-01
  • 1970-01-01
  • 2017-06-26
相关资源
最近更新 更多