2011-07-02 76 views

回答

53

如果您加載使用Visual Studio UI的資源,那麼你應該能夠做到這一點:

picturebox.Image = project.Properties.Resources.imgfromresource 
+6

+1 - 我不得不打電話'pictureBox.Load ();'顯示圖像,只從資源分配它不起作用 – Habib

0

您必須在資源文件中指定資源文件的完整路徑作爲'image'的名稱,請參閱下面的示例。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    PictureBox1.Image = My.Resources.Chrysanthemum 
End Sub 

在MyResources指定資源名稱後,分配給Image屬性的路徑。

但是在做任何事情之前,你必須從應用程序的資源部分導入圖像文件,否則它可以創建自己的。

再見

2

嘗試以下操作:

myPictureBox.Image = global::mynamespace.Properties.Resources.photo1; 

,並與您的項目命名空間

8

肯有合適的解決方案替代的命名空間,但你不希望添加的picturebox.Image。 Load()成員方法。

如果使用Load執行此操作,並且未設置ImageLocation,則它將失敗,並顯示「必須設置圖像位置」異常。如果你使用了picturebox.Refresh()成員方法,它沒有任何異常。

下面

完成代碼:

public void showAnimatedPictureBox(PictureBox thePicture) 
{ 
      thePicture.Image = Properties.Resources.hamster; 
      thePicture.Refresh(); 
      thePicture.Visible = true; 
} 

它援引爲: showAnimatedPictureBox(的MyPictureBox);

我的XAML的樣子:

<Window 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" 
     xmlns:winForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="myApp.MainWindow" 
     Title="myApp" Height="679.079" Width="986"> 

     <StackPanel Width="136" Height="Auto" Background="WhiteSmoke" x:Name="statusPanel"> 
      <wfi:WindowsFormsHost> 
       <winForms:PictureBox x:Name="myPictureBox"> 
       </winForms:PictureBox> 
      </wfi:WindowsFormsHost> 
      <Label x:Name="myLabel" Content="myLabel" Margin="10,3,10,5" FontSize="20" FontWeight="Bold" Visibility="Hidden"/> 
     </StackPanel> 
</Window> 

我意識到這是一個老帖子,但圖像直接從資源裝載是在微軟的網站非常清楚,這是我來了(部分)解決方案至。希望它能幫助別人!

+1

當儘可能使用thePicture.Invalidate()而不是刷新()。這允許應用程序在它認爲是繪圖的時候繪製它。 Refresh()強制它立即繪製它,這通常不是性能最好的... – huha

4

好......所以首先你需要在你的項目中導入圖像

1)選擇窗體設計

2)打開PictureBox的任務(這是pinted到右的小箭頭PictureBox的邊框上的圖片框)

3)點擊「選擇圖片...「

4)選擇第二個選項‘項目資源文件:’(此選項將創建一個文件夾,名爲‘資源’

5,你可以用Properties.Resources ACCES))點擊導入,並選擇你的形象從您的計算機

6)(即現在的形象與名稱相同的圖像的副本將在步驟4中創建資源文件夾發送)點擊OK

現在的形象在你的項目,你可以在屬性命令中使用它。當你想從picturebox更改圖片時,請輸入以下代碼:

pictureBox1.Image = Properties.Resources.myimage; 

注:MYIMAGE表示圖像的名稱...輸入資源後點後,在你的選擇將是你導入的圖像文件