回答
如果您加載使用Visual Studio UI的資源,那麼你應該能夠做到這一點:
picturebox.Image = project.Properties.Resources.imgfromresource
您可以使用一個ResourceManager加載圖像。
請訪問以下鏈接: http://www.java2s.com/Code/CSharp/Development-Class/Saveandloadimagefromresourcefile.htm
有趣的是從Java網站的鏈接...;)
您必須在資源文件中指定資源文件的完整路徑作爲'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屬性的路徑。
但是在做任何事情之前,你必須從應用程序的資源部分導入圖像文件,否則它可以創建自己的。
再見
嘗試以下操作:
myPictureBox.Image = global::mynamespace.Properties.Resources.photo1;
,並與您的項目命名空間
肯有合適的解決方案替代的命名空間,但你不希望添加的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>
我意識到這是一個老帖子,但圖像直接從資源裝載是在微軟的網站非常清楚,這是我來了(部分)解決方案至。希望它能幫助別人!
當儘可能使用thePicture.Invalidate()而不是刷新()。這允許應用程序在它認爲是繪圖的時候繪製它。 Refresh()強制它立即繪製它,這通常不是性能最好的... – huha
好......所以首先你需要在你的項目中導入圖像
1)選擇窗體設計
2)打開PictureBox的任務(這是pinted到右的小箭頭PictureBox的邊框上的圖片框)
3)點擊「選擇圖片...「
4)選擇第二個選項‘項目資源文件:’(此選項將創建一個文件夾,名爲‘資源’
5,你可以用Properties.Resources ACCES))點擊導入,並選擇你的形象從您的計算機
6)(即現在的形象與名稱相同的圖像的副本將在步驟4中創建資源文件夾發送)點擊OK
現在的形象在你的項目,你可以在屬性命令中使用它。當你想從picturebox更改圖片時,請輸入以下代碼:
pictureBox1.Image = Properties.Resources.myimage;
注:MYIMAGE表示圖像的名稱...輸入資源後點後,在你的選擇將是你導入的圖像文件
- 1. PictureBox更改圖像窗體資源
- 2. Picturebox的更改圖像
- 3. 更改默認圖像從資源
- 4. 使用jquery將圖像源更改爲另一圖像的源
- 5. 將PictureBox圖像從應用程序文件夾更改爲圖像
- 6. 如何更改PictureBox的圖像?
- 7. iPhone將本地圖像資源更改爲託管的URL
- 8. 更新Picturebox中的圖像
- 9. PictureBox的圖像
- 10. 如何將圖像更改爲與pictureBox相同的大小?
- 11. OnCompletion mediaPlayer不更改圖像資源
- 12. 繪製資源文件 - 更改圖像
- 13. 更改圖像src添加到資源
- 14. 圖像源更改
- 15. 更改圖像源
- 16. 將PictureBox圖像轉換爲位圖
- 17. 將圖像作爲Lotus notes中的圖像資源導入
- 18. 將Drawline圖像繪製成Picturebox圖像
- 19. 將圖像的源代碼綁定到圖像資源
- 20. C#更新PictureBox圖像
- 21. setImageResources從一個activity.java更改另一個activity.java的圖像資源
- 22. 圖像視圖設置圖像資源
- 23. 設置PictureBox的圖像圖像加載
- 24. 更改關卡圖標的圖像源
- 25. 如何將WPF引用圖像添加爲圖像資源?
- 26. 從picturebox中獲取圖像
- 27. 從Picturebox保存圖像
- 28. 如何將圖像從Picturebox(VB6中的圖像)轉換爲字符串(Base64)?
- 29. 將圖像從My.Resources加載到Picturebox(VB.NET)
- 30. WPF圖像資源
+1 - 我不得不打電話'pictureBox.Load ();'顯示圖像,只從資源分配它不起作用 – Habib