Set source of wpf image [duplicate] - wpf

This question already has answers here:
How to load image to WPF in runtime?
(2 answers)
WPF - Import image as resource
(3 answers)
Closed 1 year ago.
I have a wpf app with an image in the xaml:
<Image HorizontalAlignment="Center" Height="131" Margin="550,0,1306,-960" VerticalAlignment="Bottom" Width="58" Source="../images/blue.JPG" Name="im_Izq"/>
And I want to change the source image from the code behind.
I tried the following:
im_Izq.Source = new BitmapImage(new Uri("../../images/red.JPG"));
but, when running it, I get this error:
System.UriFormatException: 'URI no vĂ¡lido: no se puede determinar el formato del URI.'
Can you help me? Thanks!!

Make sure the image file is located in a project folder named images, and set the Build Action of the file to Resource.
im_Izq.Source = new BitmapImage(new Uri("pack://application:,,,/images/red.JPG"));
or use
im_Izq.Source = new BitmapImage(new Uri(#"/projectname;component/Images/up.ico", UriKind.Relative));

Related

XAML: how to access items from resources.resx [duplicate]

This question already has answers here:
How to get a Uri of the image stored in the resources
(2 answers)
Assign BitmapImage from Resources.resx to Image.Source?
(1 answer)
Not able to reference Image source with relative path in xaml
(3 answers)
Closed 3 years ago.
I'm learing WPF and XAML. I'm trying to show an image loaded from resources.resx.
For this, I've added an existing image to resources.resx. The identifier of the image is GoogleLogo. If I open Resources.ResX is can see that the image indeed is present in the resource file.
Now I want a window that shows this image in a Grid. The XAML is like:
<Window x:Class="WpfDemo.MainWindow"
xmlns=...
Title="Show Google Logo" Height="160" Width="800">
<Grid>
<Image Source="???"/>
</Grid>
</Window>
How to refer to the Source in resources.ResX? What to type instead of ???

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))}

Loading external images in WPF

I am building an application using WPF for Windows 8 in VB.NET and I need to load several images from the disk.
I don't want to add the images as a reference to the project, I just want to load them as is.
I already studied the "Imaging Overview" at MSDN and several other articles over the internet but nothing solved my problem.
I used both XAML and code but nothing worked. I can't get the image presented.
Dim myimage As New Image
myimage.Width = 200
Dim bitmapimage As New BitmapImage()
bitmapimage.UriSource = New Uri("C:\Users\MyName\Documents\Database\image.jpg")
myimage.Source = bitmapimage
XAML code:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Image HorizontalAlignment="Left" Height="265" Margin="191,145,0,0" VerticalAlignment="Top" Width="660">
<Image.Source>
<BitmapImage UriSource="C:\Users\MyName\Documents\Database\image.jpg"/>
</Image.Source>
</Image>
</Grid>
When I load a referenced image (ie. Assets/Logo.png) it loads it correctly.
I also added all the necessary capabilities and declerations in the application manifest.
Your XAML looks correct. Maybe the image path is invalid. However it could be written simpler like this:
<Image HorizontalAlignment="Left" Height="265" Margin="191,145,0,0" VerticalAlignment="Top" Width="660"
Source="C:\Users\MyName\Documents\Database\image.jpg" />
The path string is automatically converted to an ImageSource instance.
In code you should use the BitmapImage constructor with Uri parameter. Otherwise you would have to call BeginInit and EndInit.
Dim uri As New Uri("C:\Users\MyName\Documents\Database\image.jpg")
Dim bitmapimage As New BitmapImage(uri)
myimage.Source = bitmapimage

WPF: Dynamically loading images from the current assembly

This is my XAML
<Image Name="StatusImage" Source="/Foo.Bar.Sam;component/Images/YellowDot.png" Stretch="Fill" MaxWidth="12" MaxHeight="12">
Using VB or C#, how would I change the Image to RedDot or GreenDot?
code taken from answer to this question
string strUri2 = String.Format(#"pack://application:,,,/MyAseemby;component/resources/main titles/{0}", CurrenSelection.TitleImage);
imgTitle.Source = new BitmapImage(new Uri(strUri2));

How to display an image in Silverlight?

This should be simple, but...
I created a folder in my solution called Images. I dragged an image into it. How do I now display this image on a Page or View?
Make sure the image is set as a Resource. It can be in any folder in any of your projects in your solution.
You can then reference this as [assembly];component/[path]/[imagename.extension]
For example:
<Image Source="/mynamespace.myassembly;component/ResourcesFolder/image.png" Width="16" Height="16" />
There are a couple of ways to get at it--here's the way that involves setting the image as a Resource in the Visual Studio file properties:
using (var stream = Application.GetResourceStream(
new Uri("SilverlightAssemblyName;component/Images/myImage.png",
UriKind.Relative)))
{
// read from stream
}
Where SilverlightAssemblyName is replaced by the Assembly Name you specified in the Silverlight tab of your Silverlight project.
If you want to use the image in code:
var bitmap = new BitmapImage();
bitmap.SetSource(stream);
myImageControl.ImageSource = bitmap;
Or, if you want to use the resource in XAML, you don't need any of the code:
<Image Source="/Images/myImage.png" Width="16" Height="16" />

Resources