简体   繁体   中英

Unable to load images in Visual Studio 2015

I am having an issue with using the ImageLocation of a pictureBox. I went to:

Documents\\VisualStudios\\Projects\\Program Name:

Then I create a new folder called images and within that folder i put the pictures(rock.png , paper.png )I intend to use. Keep in mind I can not load from the C drive, I have to turn this project in, so that it can work on any computer. Am I loading my images in the wrong location? or Am i accessing them wrong?

if (PlayerOne == Rock && PlayerTwo == Scissors)
{
    ScoreOne++;
    picture1.ImageLocation = @"images\rock.png";
    picture2.ImageLocation = @"images\paper.png";
    lblOneScore.Text = Convert.ToString(ScoreOne);
    lblShowWinner.Text = " Player One Wins! ";
    picture1.Load();
    picture2.Load();
}

Instead of setting the ImageLocation property, try:

picture1.Load(@"C:\temp\pic.jpg");

The picturebox load method also accepts URL's (if you can't access drives and it needs to work on any PC without hardcoded UNC paths):

picture1.Load("http://i.stack.imgur.com/FmIGn.png");

Or use the app's location if you have packaged it with an image:

picture1.Load(Application.StartupPath + "\\a.jpg");

Or use a Resource file:

I would love to use resourceFile but I can not find it on Visual Studios 2015 only for 2013 and previous versions

It hasn't changed from VS2013 to VS2015, in your Winforms project, expand the Project Properties and double click on Resources.resx, then add an image:

在此处输入图片说明

picture1.Image = global::ProjectName.Properties.Resources.ImageName;

VisualStudios / Projects / fileName / filename / bin / Debug / create images文件夹,然后在该文件夹中插入图片,我发布的原始代码效果很好

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM