2016-12-15 59 views
1

我试图在按下按钮时更改图像框图片。如何缩短图像路径

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

如何缩短图像路径?

+0

这似乎是你的问题的标题是不同于你的实际问题。你问如何嵌入图片或如何缩短图像路径? – RandomStranger

+0

是的,我实际上如何缩短图像路径? –

回答

0

要嵌入使用Resources和访问它是这样的:

pictureBox1.Image = Resources.dice; 

要获得位于同一目录中的图片,其中程序运行:

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

@ user3060520是的,他们是相似的。但是一旦你得到'GetCurrentDirectory()'返回的“意外”路径,你就会知道它的区别。一些信息[这里](http://stackoverflow.com/questions/18727692/what-is-the-difference-between-theseways-of-getting-current-directory)。这就是为什么我通常使用程序集位置来获取当前目录。 – Reniuz

+0

@ user3060520 wheres错误应该从哪里来? – Reniuz