2009-02-28 63 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"); 
相關問題