Errors and more erros

spicyadmin

Member
Joined
Dec 23, 2021
Messages
15
Programming Experience
Beginner
I've been trying to find a solution to a problem I am having. I am trying to save a picture to the "My Pictures" folder but I keep getting an error in the code saying "cannot convert from 'string' to 'system.environment.specialfolderoption'"

What I have:
pbx_image.Image.Save(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures, "Capture-" + DateTime.Now.ToString("HH-mm-ss tt") + ".jpg"));
 
Let's try attacking this a different way. Why do you think that the code in post #9 should succeed? Explain how to you came about to writing line #7.
It should work because I want it to. idk. In #7, the mod told me to clean up my code and organize it. I did so and was then told to do the second part which would be #9, but it doesn't work.
 
Let's try attacking this a different way. Why do you think that the code in post #9 should succeed? Explain how to you came about to writing line #7.
ok, I tried the following but I get no pictures at all. There are no errors but there are also no pictures now.
C#:
        private void btn_capImage_Click(object sender, EventArgs e)
        {
            string fileName = "IPCams Capture-" + DateTime.Now.ToString("HH-mm-ss tt") + ".jpg";
           
            string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);

            string imagePath = folderPath + fileName;

            pbx_image.Image.Save(imagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
 
Change line 7, instead of concat the two strings do Path.Combine(folderPath, fileName)
 
It should work because I want it to. idk. In #7, the mod told me to clean up my code and organize it. I did so and was then told to do the second part which would be #9, but it doesn't work.
Unfortunately, we don't quite have compilers that read our minds and "do what I mean, not what I say" yet, but there are some people working hard on making that kind of AI technology available to everyone. Until then, some precision is needed in giving instructions to computers. Using a comma between imgs and saves does not automatically concatenate the two parts to make a single string. From the point of view of the compiler, you are trying to call the variant of Image.Save() that takes two parameters: a filename and an image format. It doesn't know that you were trying to call the variant that just takes a single parameter: the filename.

In post #8, @JohnH was trying to guide you towards the best way to concatenate two strings that will be parts of a file path. Recall that in most OSes, parts of the path are separated using a slash or backslash. Path.Combine() takes care of putting the appropriate separator between the strings that you pass in to it.
 
Change line 7, instead of concat the two strings do Path.Combine(folderPath, fileName)
Ok, so I did that
C#:
        private void btn_capImage_Click(object sender, EventArgs e)
        {
            string fileName = "IPCams Capture-" + DateTime.Now.ToString("HH-mm-ss tt") + ".jpg";
            
            string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);

            Path.Combine(folderPath, fileName);

        }

but what function would I now use to save? I cant use
C#:
pbx_image.Image.Save(fileName);
because It'll just save the picture to the executable folder. I want to save it to the MyPictures folder
 
Assign the result of the Combine method to a variable.
 
Change line 7, instead of concat the two strings do Path.Combine(folderPath, fileName)
GOT IT!!! Thank you for the help!!!

C#:
        private void btn_capImage_Click(object sender, EventArgs e)
        {
            string fileName = "IPCams Capture-" + DateTime.Now.ToString("HH-mm-ss tt") + ".jpg";
            
            string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);

            string pics = Path.Combine(folderPath, fileName);

            pbx_image.Image.Save(pics);
        }
 
You should also specify ImageFormat.Jpeg like you did in post 17, else you may end up with a bmp, png or something else with wrong file extension .jpg.
 
It should work because I want it to.
LOL 🤣 Maybe there is a compiler or runtime /dowhatiwant switch or someting alike ?

But seriously. What part of Path.Combine do you not understand ? It combines (in this case) a folder name and a file name, and returns the complete path. That complete path must the argument to your Save() call. Calling Path.Combine() without using its return value is pointless. I hope that helps at last ! Oh wait, I now see you have already sorted it out with JohnH's help.
 

Latest posts

Back
Top Bottom