2013-03-16 37 views
1

我试过找到解决方案,但无济于事。我相信我想要的是字符串连接...Visual Basic 2010,Picturebox资源&&变量?

我有一个变量"pictureid=face2" 在我的资源文件夹中我有一张图片"face2.jpg"。 在窗体上我有一个图片框。

这是代码我不能去上班

pictureBox1.Image = Properties.Resources.(pictureid + ".jpg"); 

我在哪里去了?错误表示存在预期的标识符。

+0

您的意思是'Visual Studio的2010'? – 2013-03-16 23:27:52

回答

1

Image期待一个Image or a descendant thereof(位图和图元文件对象),你可以用你的编码,如果你的图像添加到您的项目资源(编辑:我要澄清 - 要做到这一点,到项目>属性>资源选项卡和“添加资源”不要只是拖放到文件夹中)

pictureBox1.Image = Properties.Resources.face2; 

如果您不想在项目中包含的图片,你可以使用ImageLocation,这将接受字符串而不是对象:

pictureBox1.ImageLocation = pictureid + ".jpg"; //assuming you include it in the same folder as the exe 

你也可以做这样的事情:

Image face2 = Image.FromFile(pictureid + ".jpg"); 
pictureBox1.Image = face2; 
+0

非常感谢!我不知道我可以参考“debug/bin”作为根文件夹! – 2013-03-17 00:17:10

+1

pictureBox1.ImageLocation = pictureid +“.jpg”;为我工作很好!对不起,我会答复,但我需要至少15点?!在这个网站上的积分系统感觉有点像一场比赛给我......人们沉迷于有多少“点”,有..叹息.... 感谢所有雷尼! – 2013-03-17 00:18:04

+0

是的 - 除非一种方法需要一个完全合格的路径,否则它将假定Environment.CurrentDirectory。很高兴听到它的工作;随时标记为答案,代替投票。 – raney 2013-03-17 01:46:30