【问题标题】:Visual Basic 2010, Picturebox resource && Variable?Visual Basic 2010,Picturebox 资源 && 变量?
【发布时间】:2013-03-16 23:20:19
【问题描述】:

我已尝试找到解决方案,但无济于事。我相信我想要的是字符串连接......

我有一个变量"pictureid=face2" 在我的资源文件夹中,我有一张名为"face2.jpg" 的图片。 在表单上我有一个图片框。

这是我无法工作的代码

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

我哪里出错了?该错误表示需要一个标识符。

【问题讨论】:

  • 你的意思是Visual Studio 2010

标签: c# variables picturebox string-concatenation


【解决方案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;

【讨论】:

  • 非常感谢!我不知道我可以将“debug/bin”称为根文件夹!
  • pictureBox1.ImageLocation = pictureid + ".jpg";对我来说很棒!对不起,我会回答,但我至少需要 15 分?!这个网站上的积分系统对我来说有点像一个游戏......人们沉迷于有多少“积分”......叹息......感谢所有相同的雷尼!
  • 是的 - 除非方法需要完全限定的路径,否则它将假定 Environment.CurrentDirectory。很高兴听到它起作用了;随意标记为答案,以代替投票。
猜你喜欢
  • 2015-06-27
  • 2011-08-06
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
  • 2015-12-02
  • 1970-01-01
  • 1970-01-01
  • 2011-11-18
相关资源
最近更新 更多