none
Save images into file was denied RRS feed

  • Question

  • I try to save List<images> img in images folder

    But it show System.UnauthorizedAccessException: 'Access to the path 'C:\A\Images' is denied.'

    string root = @"C:\A\Images"; // If directory does not exist, create it. if (!Directory.Exists(root)) { Directory.CreateDirectory(root); for (int i = 0; i < ICounts; i++) { var encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create((BitmapSource)img[i].Source)); using (FileStream stream = new FileStream(root, FileMode.Create)) encoder.Save(stream); }//end for loop }//end if else if (Directory.Exists(root)) {

    }//end else if

    My img was List<Image> was dynamicly allocate depend on my need

    ICounts was numbers of my img.

    I have try WriteableBitmap   but I am not sure how to use it .

    so I try    FileStream.

    How to solve this Access to the path 'C:\A\Images' is denied. problem


    • Edited by CY_Chen Wednesday, August 14, 2019 8:32 AM
    Wednesday, August 14, 2019 8:28 AM

Answers

  • I try to save List<images> img in images folder

    But it show System.UnauthorizedAccessException: 'Access to the path 'C:\A\Images' is denied.'

    string root = @"C:\A\Images"; // If directory does not exist, create it. if (!Directory.Exists(root)) { Directory.CreateDirectory(root); for (int i = 0; i < ICounts; i++) { var encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create((BitmapSource)img[i].Source)); using (FileStream stream = new FileStream(root, FileMode.Create)) encoder.Save(stream); }//end for loop }//end if else if (Directory.Exists(root)) {

    }//end else if

    My img was List<Image> was dynamicly allocate depend on my need

    ICounts was numbers of my img.

    I have try WriteableBitmap   but I am not sure how to use it .

    so I try    FileStream.

    How to solve this Access to the path 'C:\A\Images' is denied. problem


    Hi    CY_Chen,


     I tried the following code, It is works well. You can refer it

                 string root = @"C:\A\Images";
    
                // If directory does not exist, create it. 
    
                if (!Directory.Exists(root))
                {
                    Directory.CreateDirectory(root);
                    for (int i = 0; i < 5; i++)
                    {
                        JpegBitmapEncoder bitmapEncoder = new JpegBitmapEncoder();
                        bitmapEncoder.Frames.Add(BitmapFrame.Create(rtbitmap));
                        bitmapEncoder.Save(File.OpenWrite(root + "\\"+i.ToString()+".jpg"));
                    }
                      
                }
    


    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CY_Chen Thursday, August 15, 2019 3:59 AM
    Thursday, August 15, 2019 3:15 AM
    Moderator