How do I set a PictureBox image to an image from my resources?
(I tried this without success: pictuerbox.Image = "img_location";
)
How do I set a PictureBox image to an image from my resources?
(I tried this without success: pictuerbox.Image = "img_location";
)
If you loaded the resource using the visual studio UI, then you should be able to do this:
picturebox.Image = project.Properties.Resources.imgfromresource
Ken has the right solution, but you don't want to add the picturebox.Image.Load() member method.
If you do it with a Load and the ImageLocation is not set, it will fail with a "Image Location must be set" exception. If you use the picturebox.Refresh() member method, it works without the exception.
Completed code below:
public void showAnimatedPictureBox(PictureBox thePicture)
{
thePicture.Image = Properties.Resources.hamster;
thePicture.Refresh();
thePicture.Visible = true;
}
It is invoked as: showAnimatedPictureBox( myPictureBox );
My XAML looks like:
<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>
I realize this is an old post, but loading the image directly from a resource is was extremely unclear on Microsoft's site, and this was the (partial) solution I came to. Hope it helps someone!
Ok... So, first you need to import the image into your project
1). Select the picturebox in Form Design
2). Open PictureBox Tasks (it's the little arrow pinted to right on the edge on the picturebox)
3). Click on "Choose image..."
4). Select the second option "Project resource file:" (this option will create a folder called "Resources" which you can access with Properties.Resources)
5). Click on import and select your image from your computer (now a copy of the image with the same name as the image will be sent in Resources folder created at step 4)
6). Click on Ok
Now the image is in your project and you can use it with the Properties command. Just type this code when you want to change the picture in the picturebox:
pictureBox1.Image = Properties.Resources.myimage;
Note: myimage represent the name of the image...after typing the dot after Resources, in your options it will be your imported image file.
You can use a ResourceManager to load the image.
See the following link: http://www.java2s.com/Code/CSharp/Development-Class/Saveandloadimagefromresourcefile.htm
try the following:
myPictureBox.Image = global::mynamespace.Properties.Resources.photo1;
and replace namespace with your project namespace
You must specify the full path of the resource file as the name of 'image within the resources of your application, see example below.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.Image = My.Resources.Chrysanthemum
End Sub
In the path assigned to the Image property after MyResources specify the name of the resource.
But before you do whatever you have to import in the resource section of your application from an image file exists or it can create your own.
Bye
© 2022 - 2024 — McMap. All rights reserved.
pictureBox.Load();
to display the image, only assigning it from resources didn't work – Evenfall