简体   繁体   中英

How can I make shorter imagepath

I'm trying to change the imagebox picture when the button is pressed.

if (label1.Text=="1")
{
    pictureBox1.Image=Image.FromFile("C:/Users/Usr/Documents/Visual Studio 2010/Projects/randomly/randomly/Resources/dice1.jpg");
} 

How can I make a shorter image path?

To embed use Resources and access it like this:

pictureBox1.Image = Resources.dice;

To get pictures located in the same directory where your program is running:

string pathToProgramDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
string pathToPicture = Path.Combine(pathToProgramDirectory, "dice1.jpg");
pictureBox1.Image = Image.FromFile(pathToPicture);

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