How to set ImageSource in a codebehindfile - wpf

I have in a UserControl the Property ImageSource. How can I set the ImageSource in my code behind to a Image in my Resources directory?
I want to bind the Image Source to the property ImageSource.
<Image Source="{Binding Path=ImageSource}" />

In order to create a BitmapImage (which is derived from ImageSource) from a resource file in code, you would do the following, provided that the file MyImage.jpg is in a folder named Images of your Visual Studio project, and that its Build Action is set to Resource:
var uri = new Uri("pack://application:,,,/Images/MyImage.jpg");
ImageSource = new BitmapImage(uri); // set the ImageSource property
See also this answer.
You could also directly use the image resource without binding:
<Image Source="/Images/MyImage.jpg" />

Related

XamlWriter image serialization isssue (InvalidOperationException: Property 'UriSource' or property 'StreamSource' must be set.)

i am using a ink canvas and added a image to it and then serialize using XamlWriter.
Image img = new Image();
BitmapImage img = NImageHelper.GetBitmapImageFromString(ImageData, new Size(src.Width, src.Height));
img.Source = img;
string Xml = XamlWriter.Save(myCanvas);
this code works fine, Here is the xml generated
<Image Height="100" Width ="150">
<Image.Source>
<BitmapImage CacheOption="None" CreateOptions="IgnoreImageCache" BaseUri="{x:Null}"/>
</Image.Source>
</Image>
In this xml bitmapimage's StreamSource property is not serialized
When i try to parse it back it is throwing exception
System.Windows.Markup.XamlParseException: ''Initialization of 'System.Windows.Media.Imaging.BitmapImage' threw an exception.' InvalidOperationException: Property 'UriSource' or property 'StreamSource' must be set.
It works fine if the bitmapimage's UriSource property is set and i am able to parse it back.
but i don't want to set UriSource property.
Please help me , how to serialize StreamSource property?
Thanks in advance.

Using Bitmap Class in XAML [duplicate]

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.

Convert System.Drawing.Image resource into System.Windows.Controls.Image for wpf MenuItem

I've got resources in my assembly which I can Access using Properties.Resources.MyImage.
And I have some class which I bind to a WPF MenuItem containing a property
public System.Windows.Controls.Image Icon {get; set;}
This I want to set programmatically using:
dummy.Icon = Properties.Resources.MyImage;
Now I want to convert the resource System.Drawing.Image to the WPF System.Windows.Controls.Image. I thought this should be straightforward, but I found no working solution for my Images (which are png files using transparency).
So how do I convert System.Drawing.Image into System.Windows.Controls.Image?
Instead of using Properties.Resources, which are Windows Forms Embedded Resources, use WPF resources. In Solution Explorer, click the image file and in the properties window, set its Build Action to Resource (not Embedded Resource). This also embeds the image into the assembly, but in a different way.
Unlike Windows Forms, WPF does not generate a resource manager class, so you'd have to use strings to load the images dynamically:
BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri("pack://application:,,,/NameOfAssembly;component/Path/To/Image.png");
image.EndInit();
Note the application and component parts of the URI are constant strings, while NameOfAssemly is the name of the assembly where the image is in. You can build a helper class that builds the URI and loads images.
You can also call image.Freeze() if you don't plan on making any changes to the image (improves performance and allows image source to be created on non-UI threads).
In your data class, expose an ImageSource property instead of an Image. Then you use the Image control to display it:
<Image Source="{Binding Icon}" />
Or inside a style:
<Style TargetType="MenuItem">
<Style.Resources>
<Image x:Key="Icon"
x:Shared="False"
Source="{Binding Icon}"
Width="16"
Height="16" />
</Style.Resources>
<Setter Property="Icon" Value="{StaticResource Icon}" />
</Style>

Access icon resources through URI VB.NET

I have a WPF VB.NET application and I want to use an icon embedded in the applications resources as a menu icon. So far I have this code (in the window's initialized event):
MenuItem.Icon = New Image() With {.Source = New BitmapImage(New
Uri("Resources\Icon.ico", UriKind.Relative))}
And the icon is still not displayed, any ideas?
The problem is your URI. If you set it in code behind, you must write the full WPF Pack URI. You must also set the Build Action
of the icon file to Resource (the default value for icons is None).
MenuItem.Icon = New Image() With
{
.Source = New BitmapImage(New Uri("pack://application:,,,/Resources/Icon.ico"))
}
When you specify the URI in XAML, the default ImageSource TypeConverter will add the pack://application:,,, part, and you could simply write
<Image Source="/Resources/Icon.ico"/>
Better option is building menu in XAML:
Create folder Images in your solution
Add image as Resources to Images directory (in my sample code: "Icon.ico")
In XAML you can use following code:
...
<MenuItem Header="Item1">
<MenuItem.Icon>
<Image Source="/Images/Icon.ico" Width="20" Height="20" />
</MenuItem.Icon>
</MenuItem>
Or if you want to do this in code-behind you can use following code instead of step 3:
MenuItem.Icon = New Image() With {.Source = New BitmapImage(New Uri("/Images/Icon.ico", UriKind.RelativeOrAbsolute))}

binding an image source in XAML

I am trying to bind an image source to my XAML through c#
this works
<Image Source="images/man.jpg"></Image>
this does not work
<Image Source="images/{Binding imagesource}"></Image>
where imagesource is a string variable in the c# file of this xaml and is being set equal to "man.jpg"
here is a way how to do it in XAML:
add this to the namespace:
xmlns:System="clr-namespace:System;assembly=mscorlib"
then add your images paths
<System:String x:Key="ImageRefresh">/Theme;component/Images/icon_refresh.png</System:String>
<System:String x:Key="ImageSearch">/Theme;component/Images/icon_search.png</System:String>
This is how you use it
<Image Height="16" Source="{StaticResource ImageSearch}" Stretch="Uniform" Width="16"/>
This works ok, but if you load your xaml style in Blend it will go bogus..
An object of type "System.String" cannot be applied to a property that expects the type "System.Windows.Media.ImageSource".
I haven't figured out yet, how to replace System:String with that Media.ImageSource... but hey.. it works for me in Visual Studio.
You can't stick a binding mid-way through the value like that. It's either a binding, or it's not. Assuming imagesource is publicly accessible via your DataContext, you could do this:
<Image Source="{Binding imagesource}"/>
However, if it's been set to "man.jpg" then it won't find the image. Either set imagesource to the full path ("images/man.jpg") or use a converter:
<Image Source="{Binding imagesource, Converter={StaticResource RelativePathConverter}}"/>
The converter would prepend "images/" onto its value. However, it may be necessary for the converter to return an ImageSource rather than a string.
Images have bitten me in the past. There is a certain lookup order involved.
When you use "image/man.jpg" it could refer to a file inside your silverlight xap, or relative to the location of XAP file. For example, it could be in YourProject.Web/ClientBin/image/man.jpg.
You should troubleshoot by using full URLs first and find out if this works.
imagesource needs to be an actual Image object, not a string.
Here is a method that will create a new Image object given a path:
public BitmapImage Load(string path)
{
var uri = new Uri(path);
return new BitmapImage(uri);
}

Resources