2009-02-28 62 views

回答

25

考虑使用Properties.Resources.yourImage

Properties.Resources包含您添加作为资源(看你的项目属性,资源选项卡)一切

除此之外,如果您嵌入在项目中的图像作为资源,您可以通过呼叫GetManifestResourceStream他们得到大会,你已经嵌入图像,东西像

Stream imgStream = 
    Assembly.GetExecutingAssembly().GetManifestResourceStream(
    "YourNamespace.resources.ImageName.bmp"); 
pictureBox.Image = new Bitmap(imgStream); 

不要忘记将图像标记为嵌入式资源! (您需要在其属性窗口中设置图像的构建动作)

如果您发现自始至终从GetManifestResourceStream回收null,您可能会输入错误的名称。 (可能很难得到正确的名称)在组件上致电GetManifestResourceNames;这会给你所有的资源名称,你可以在列表中找到你需要的。

2
string img = null; 

private void btnShow_Click(object sender, EventArgs e) 
{ 
    string imgName; 
    img = textBox1.Text; 
    imgName = "images/" + img + ".jpg"; 

    if (imgName == null) 
    { 
     MessageBox.Show("no photo"); 
    } 
    else if (imgName != null) 
    { 
     this.picBox1.Image = Image.FromFile("images/" + img + ".jpg"); 
    } 
} 
1

以下是从资源文件夹中获取图像的代码。通常我们将图像保存在资源文件夹中。但有时我们只有我们的图像名称。在这种情况下,您只能使用图像名称从资源文件夹访问图像。

下面的代码将演示关于它。

private System.Resources.ResourceManager RM = new System.Resources.ResourceManager("YourAppliacationNameSpace.Properties.Resources", typeof(Resources).Assembly); 

PbResultImage.Image = (Image)RM.GetObject(YourPictureName); 
  • YourAppliacationNameSpace意味着你的应用程序的名称。
  • YourPictureName表示您想从资源文件夹访问的图片。但图片名称必须没有扩展名,例如(PNG,GIF,JPEG等)

希望我会对某人有利。

谢谢。

0

萨拉姆。

为我工作:

(Bitmap) Properties.Resources.ResourceManager.GetObject("ImageName"); 
相关问题