开发者

Whats wrong displaying this image?

开发者 https://www.devze.com 2023-02-01 22:18 出处:网络
PictureBox image = new PictureBox(); image.Image = new Bitmap(\"grass.jpg\"); In my project, I added this image named grass.jpg.

PictureBox image = new PictureBox();
image.Image = new Bitmap("grass.jpg");

In my project, I added this image named grass.jpg. This code throws this exception at run time: "Parameter is not val开发者_Go百科id."


In the properties of "grass.jpg" set Copy To Output Directory to Copy if newer. This will copy the image to the current directory of the program. The path "grass.jpg" is relative to the program, i.e. it is a file in the same directory as the program.

If you want to specify an absolute path to the image. You can use the path to the executable to build a path to the image like so:

string myDirectory = Path.GetDirectoryName(Application.StartupPath);
string imageFile = Path.Combine(myDirectory, "grass.jpg");

(But this is obviously the same path)


Probably the image couldn't be found. Try to specify a full path.

0

精彩评论

暂无评论...
验证码 换一张
取 消