Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to create a method which loads a image (png) and stores it in a Bitmap variable to be used later. The code is as follows:

C#
private static Bitmap LoadImage(string subfolderName, string imageName) {
           string fileSpec = string.Format(@".\Images\{0}\{1}.png", subfolderName, imageName);

           Bitmap bitmap = new Bitmap(fileSpec);


           return bitmap;
       }

As you can see its fairly simple, I store the file path in fileSpec and then use that as the parameter for creating the new bitmap. The issue is when I use this is that I get the following error:

C#
"Parameter is not Valid"

The error points to this line:

C#
Bitmap bitmap = new Bitmap(fileSpec);  


I have found that this is error is meant to happen when a file is non-existent, but the file is there and when I use a static file address such as this:

C#
private static Bitmap LoadImage(string subfolderName, string imageName) {

            string st = @".\Images\Cards\CardBack_Red.png"; //did this to test if it was working in any capacity at all

            Bitmap bitmap = new Bitmap(st);

            return bitmap;
        }

I get no error and image loads fine. Am I missing something really obvious? Help would really be appreciated, I'm getting really frustrated and desperate at this point.

Thanks.
Posted
Updated 29-May-15 21:54pm
v2
Comments
Abhipal Singh 30-May-15 3:02am    
Did you debugged the code?
What are the values of input parameters subfolderName and imageName when you debug?
Most importantly, What is the value assigned to fileSpec?

below line looks perfect..
string fileSpec = string.Format(@".\Images\{0}\{1}.png", subfolderName, imageName);
tetsuoandyouth 30-May-15 3:20am    
Example input would be subfoldername = "Cards" imageName = "CardBack_Red". The filespec value comes out to be: "\\Images\\Cards\\CardBack_Red.png". I have been stuck on problem for the entire day, but as of yet I have no solution
Abhipal Singh 30-May-15 3:29am    
Did you checked the values in variables, by attaching a break point in visual studio?

Also, the "." is missing in "\\Images\\Cards\\CardBack_Red.png"
it should be ".\\Images\\Cards\\CardBack_Red.png"
tetsuoandyouth 30-May-15 3:38am    
Yes I've tried using breakpoints and have also added '.' back to filespec, but issue persists. Any suggestions?
Abhipal Singh 30-May-15 3:46am    
Your comment says that the file name is "CardBack_Red.png"

However, you are hardcoding file name as "CardBackRed.png" in your question.

Can you check that as well.

1 solution

Start by doing two things.
First, change your code slightly:
C#
string fileSpec = string.Format(@".\Images\{0}\{1}.png", subfolderName, imageName);
Becomes:
C#
string fileSpec = Path.Combine(@".\Images", subfolderName, imageName + ".png");
This removes any chances that your parameters already have trailing "\" characters which are messing up the resulting path.
Then use the debugger to look at exactly what string that generates - if should be identical to the manually entered string you show.

Second, consider using a better path specification: relative paths can get very confused if the conditions your code is running under change even slightly. If you are ever writing these files from within you application, then consider moving them to a "writable" folder: Where should I store my data?[^] should help.
If you don't write them, and these are readonly, then consider using
C#
string fileSpec = Path.Combine(Application.StartupPath, @"Images", subfolderName, imageName + ".png");
 
Share this answer
 
Comments
tetsuoandyouth 30-May-15 4:21am    
Thanks for the reply! It worked! Thanks so much!
OriginalGriff 30-May-15 4:38am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900