Click here to Skip to main content
11,480,626 members (58,938 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
how could i solve this problem "Parameter is not valid" when set image as background of form .
this.BackgroundImage = new Bitmap(@"..\pic\background.jpg");
Posted 14-Dec-12 8:07am
Comments
jibesh at 14-Dec-12 13:16pm
   
can you post the full exception message.

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

this is the full exception message .
jibesh at 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'
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.backgroundimage.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1[^],
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx[^].

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.

—SA
  Permalink  
Comments
__TR__ at 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 at 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.
—SA
__TR__ at 14-Dec-12 16:14pm
   
Totally agree.
Sergey Alexandrovich Kryukov at 14-Dec-12 17:22pm
   
Great.
Cheers,
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
C:\Samples\Pic\background.jpg
  Permalink  
Comments
azizamustafa at 14-Dec-12 13:33pm
   
thank you all very much . your answers help me very much and the problem solved . thanks all
jibesh at 14-Dec-12 13:34pm
   
Great!!! happy to hear that. you can mark this solution as accepted so that others refer it in future.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
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).
Regards.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 294
1 Richard MacCutchan 150
2 OriginalGriff 145
3 CHill60 130
4 CPallini 125
0 Sergey Alexandrovich Kryukov 8,184
1 OriginalGriff 7,556
2 Sascha Lefèvre 3,114
3 Maciej Los 2,491
4 Richard Deeming 2,335


Advertise | Privacy | Mobile
Web02 | 2.8.150520.1 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100