2

I have a program that saves images, but when I debug it I get this error:

The given path's format is not supported.

I want to know why it is not supported, and how to fix this issue. Thanks in advance

My Code

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

Edit

I have added the code, and it compiles correctly, but the file never saves. Here's the code:

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

Edit

I have added all of the below code, but I still get the The given path's format is not supported. error. Thanks again.

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
                    }
                }
            }
Liam McInroy
  • 4,339
  • 5
  • 32
  • 53
  • Maybe this will help you. http://stackoverflow.com/questions/7348768/the-given-paths-format-is-not-supported-c-sharp – lionheart May 07 '12 at 02:32

2 Answers2

2

Always make path like

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

There are quite a few areas from where this problem is generated.

1. Make sure that path is created.

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

2. Maybe directory is already created but you do not have rights being C:. Change C: to D:

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

Now check if it created there.

Read more on this Save Method.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
-1

Try using a filename:

string file_name = "c:\\example\\path\\afile.jpg";
McGarnagle
  • 101,349
  • 31
  • 229
  • 260