FindResource returns empty Image - c#

In the main window XAML I added resource Image my_image.
In the code, find its function FindResource
The function returns a non-null.
But inside the img.Source is empty.
What am I doing wrong?
//xaml
<Window.Resources>
<Image x:Key="my_image" Source="Properties/images/device1.png"/>
</Window.Resources>
//c# code
Image img=this.FindResource("my_image") as Image;
UPDATE:
The problem was solved by pointing assembly type as a resource.
And also had to create a new instance of the Image img_new.
And give it a Source of what has been obtained from resources.
Then to normal, we can work with img_new.
Image img=this.FindResource("my_image") as Image;
Image img_new=new Image();
img_new.Source=img.Source;

Change the Build Action of your device1.png to Resource from Content

Related

ImageBox insert image without path

I would like to add the image directly to the image box, so imgVarschaubild.Source = sa;
Unfortunately this does not work, how do I do it? Without which I have to save the picture first but must directly insert the picture?
My solution at the moment:
System.Drawing.Image sa;
....
if (saveFileDialog.ShowDialog() == true)
{
sa.Save(saveFileDialog.FileName);
MessageBox.Show(saveFileDialog.FileName);
ImageSource imageSource = new BitmapImage(new Uri(saveFileDialog.FileName));
imgVorschaubild.Source = imageSource;
}
System.Drawing.Image is a Windows Form class. You could try to convert it to a BitmapSource using any of the suggestions and code samples from here:
Show Drawing.Image in WPF
Once you have done that you could set the Source property of the Image element to the BitmapSource:
imgVorschaubild.Source = GetImageStream(sa);
It seem's like you are not using the MVVM pattern. I hardly recommend it.
If you use it you could easily bind your imagePath to an Image.
You just need a string property in your ViewModel and bind to it in your View like
<Image Source="{Binding ImagePath}" />

Get Source URI of Image from code behind

I have an Image declared and Source specified in XAML with this code -
<Image x:Name="imgSomeFile" Source="Assets/someFile.png"/>
Is there a way I can access this source URI in the code-behind using only the name of the instance ? I've tried image.Source but that only gives the BitmapImage that the image uses and not the URI.
Use the UriSource property of the BitmapImage you get from the source.
((imgSomeFile).Source as BitmapImage).UriSource.OriginalString

How to get a Uri of the image stored in the resources

I have two .png files added to my resources which I need to access their Uri when doing binding.
My xaml code is as followed:
<Grid>
<Image>
<Image.Source>
<BitmapImage DecodePixelWidth="10" UriSource="{Binding Path=ImagePath}"/>
</Image.Source>
</Image>
</Grid>
and the binding code using ImagePath is:
ImagePath = resultInBinary.StartsWith("1") ? Properties.Resources.LedGreen : Properties.Resources.ledRed;
However
Properties.Resources.LedGreen
returns a Bitmap instead of String containing the Uri of that particular image.
I just want to know how to extract that value without a need to address a path of the image in the directory that it's stored. (Which honestly I am not sure is a right thing to do as I couldn't find any similar situation on the net).
Please let me know if there is even a preferred method to the one I am trying to use if available.
In a WPF application you would usually not store images in Properties/Resources.resx and access them by means of the Properties.Resources class.
Instead you just add the image files to your Visual Studio project as regular files, perhaps in a folder named "Images" or the like. Then you would set their Build Action to Resource, which is done in the Properties window. You get there e.g. by right-clicking the image file and select the Properties menu item. Note that the default value of the Build Action should be Resource for image files anyways.
In order to access these image resources from code you would then use a Pack URI. With the above folder name "Images" and an image file named "LedGreen.png", creating such an URI would look like this:
var uri = new Uri("pack://application:,,,/Images/LedGreen.png");
So you could perhaps declare your property to be of type Uri:
public Uri ImageUri { get; set; } // omitted INotifyPropertyChanged implementation
and set it like this:
ImageUri = resultInBinary.StartsWith("1")
? new Uri("pack://application:,,,/Images/LedGreen.png")
: new Uri("pack://application:,,,/Images/LedRed.png");
Finally your XAML should look like shown below, which relies on built-in type conversion from Uri to ImageSource:
<Grid>
<Image Width="10" Source="{Binding Path=ImageUri}" />
</Grid>
Declare the Properties.Resources.LedGreen property as ImageSource and set it to Uri location rather than the Bitmap object.
Or if you insist of storing it as a bitmap you can get the source by returning Properties.Resources.LedGreen.ImageSource which will be of type ImageSource.
I would prefer the first approach.

WPF Binding image source from Project Resources

Ok i have in my project Resources about 5 Images. What i want to do is to Bind an Image.Source from my Project Resources. Im C# code its pretty easy, i just do :
ImageHolder.Source = Propetries.Resources.Image1.png.
How can this be done in XAML?
Something like this :
<Image Source={??????}/>
Thanks in advance.
Visual studio will create Resources folder and put your image file into it when you add image to the resx file.
In order to use this image in binding you will need to change build action from None to Resource. After that you can bind as follows:
<Image Source="Resources/your_image_name.png"/>
You can not bind directly to Propetries.Resources.your_image_name because of you will need to convert System.Drawing.Bitmap to WPF BitmapSource. But you can bind to strings in the Resource.resx:
<TextBlock Text="{x:Static properties:Resources.YourStringResource}"></TextBlock>
Read here how to convert System.Darwing.Bitmap to the WPF bitmap: Load a WPF BitmapImage from a System.Drawing.Bitmap
And here about binding to the values in the resx file: Get values from *.resx files in XAML
Make sure your Build Action for image is marked as Resource and then you can simply do this in your XAML -
<Image Source="Properties/Resources/a.png"/>
Assuming Propetries/Resources is folder structure in your project where your image is present.

Change PictureBox's image to image from my resources?

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 in your project the image
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 acces 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 Properties command.Just type this code when you want to change the picture from 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
try the following:
myPictureBox.Image = global::mynamespace.Properties.Resources.photo1;
and replace namespace with your project namespace
You can use a ResourceManager to load the image.
See the following link:
http://www.java2s.com/Code/CSharp/Development-Class/Saveandloadimagefromresourcefile.htm
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

Categories

Resources