Click here to Skip to main content
15,742,477 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how could i solve this problem "Parameter is not valid" when set image as background of form .
this.BackgroundImage = new Bitmap(@"..\pic\background.jpg");
Jibesh 14-Dec-12 13:16pm    
can you post the full exception message.

The code and the parameter you passed looks ok to me.
azizamustafa 14-Dec-12 13:19pm    
Parameter is not valid

this is the full exception message .
Jibesh 14-Dec-12 13:22pm    
sounds your there wont be any folder call Pic or any image name with background.jpg

try giving full path "C:\\Pic\background" for debugging instead of ..\Pic\background.jpg'

Here is what I do instead of looking to the path of the image:

Add that image as a resource in the project:

Then add a property in a object class to house the get/set of that image. (optional)

then you can call the class that the image is get/set in and then the image name that will collect it.

that should change this:
this.BackgroundImage = new Bitmap(@"..\pic\background.jpg");.

to this:
this.BackgroundImage = new Bitmap([...appname].Properties.Resources.[imagename]).

[...appname] should be replaced with the main namespace of your applicationname.
[imagename] should be replaced with the name you assigned to that image when you added to the resoureces.

I had the same problem that I fixed but if you use the application on a different machine then that path could be different if your pulling from an in general file and avoids looking into the bin file statically.

Hope that helps.
Share this answer
Argument exception is because your application failed to locate the file or folder you passed as the argument.

If you use "..\Pic\fileName" means the relative path will be something like this

eg: your executable is running from C:\Samples\Executables\myProject.exe

when you run your application , its trying to load the image from
Share this answer
azizamustafa 14-Dec-12 13:33pm    
thank you all very much . your answers help me very much and the problem solved . thanks all
Jibesh 14-Dec-12 13:34pm    
Great!!! happy to hear that. you can mark this solution as accepted so that others refer it in future.
The code you show is correct, except that there are no situations where a hard-coded file path can be useful, absolute or relative. File paths are always calculated during runtime based on user input, some configuration data, assembly location or "special folders" calculated based on directories associated with a user of for "all users". The problem is somewhere else.

It could be
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(/* ... */);


myForm.BackgroundImage = bitmap;

Please see:[^],[^],[^],[^].

You could do something else, in a wrong way. Next time, please make sure you provide relevant code and comprehensive issue report, including exact error or exception message and type, and, importantly, indicate the line of code related to the error or exception.

Share this answer
__TR__ 14-Dec-12 15:29pm    
My 5!
I agree with the point that its never a good idea to hard code file path in the code.
Sergey Alexandrovich Kryukov 14-Dec-12 15:37pm    
Thank you. I insist that this is not just the bad idea, it actually cannot work if an application is deployed. The system does not have any predefined directories. On one of my fully functional systems even the disk "C" is absent; and it happened not because I wanted to do the trick, but naturally, as a result of upgrade. No hard-coded path name can possibly work in a universal way.
__TR__ 14-Dec-12 16:14pm    
Totally agree.
Sergey Alexandrovich Kryukov 14-Dec-12 17:22pm    
Syntax is correct. Issue should be that the specified file does not exist in the specified path. Please verify the file name and path again. Path is relative to the executable location (make sure whether debug folder or release folder that you target).
Share this answer

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

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