Get Source URI of Image from code behind - c#

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

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}" />

How to 'Name' a BitmapImage

For debugging purposes I'd like to set a BitmapImage's Name property. Unfortunately it doesn't exit. Is there any way to identify a specific BitmapImage when setting a breakpoint? In Winforms, all Controls have a Name property, and even if they didn't, we could use the Tag property. For BitmapImage, on the other hand, I can't find anything comparable.
To clarify: I need something defining the BitmapImage itself, not the variable pointing to it, so that if I have bi2 = bi1 - I can check if bi2 is this BitmapImage.
You can use x:Name to define a name of any object in XAML. Then you can use it for example for the debugging purposes.
<Image>
<Image.Source>
<BitmapImage x:Name="MyBitmapImage"/>
</Image.Source>
</Image>

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.

FindResource returns empty Image

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

Categories

Resources