Change image source using C# - c#

I'm creating a windows store app and was wondering how to change the image source using C# I have included the following directive;
using Windows.UI.Xaml.Media.Imaging;
and this is the line I have to try and change the image
cloud.Source = new BitmapImage(new Uri("Assets/cloud1.png", UriKind.Relative));
cloud is the name of the image tag and cloud1 is the source I want to change it to.. but it gives me this error...
"The given System.Uri cannot be converted into a Windows.Foundation.Uri"
Any help appreciated! thanks in advance

Use
cloud.Source = new BitmapImage(new Uri("ms-appx:/Assets/cloud1.png", UriKind.Relative));
instead.

Related

My WPF UserControl can't locate resources when I use it with a project other than the one it was built in

So I built a WPF user control as part of a larger solution. I'm building another app where this user control would work nice. I added a reference to the DLL from the new app, wired it up and compiled. The window loads and I can see the image that it's telling me it can't find. After the main window displays and the user control is populated, an exception is thrown saying...
System.IO.IOException: 'Cannot locate resource 'resources/nosortnofilter.png'.'
The user control is a DataGrid with some extensions added to it. The column it threw on was "id". As you can see in the image, the red arrow shows the nosortnofilter.png image being displayed in all columns. So why is it throwing this exception?
The line of code it throws on is here.
If ImageName = "" Then ImageName = "NoSortNoFilter"
img.Source = New BitmapImage(New Uri("pack://application:,,,/Resources/" & ImageName & ".png"))
So it all looks good from my perspective. Hoping someone can see what I'm not seeing.
EDIT: Found a solution. This works. But it still doesn't answer the questions why the original pack:// formatted URI only worked with the original solution.
img.Source = New BitmapImage(New Uri($"Resources/{ImageName}.png", UriKind.Relative))
EDIT: Thanks to rfmodulator for giving me the correct URI for DLL's.
img.Source = New BitmapImage(New Uri("pack://application:,,,/AdvancedSortFilterDataGrid;component/Resources/" & ImageName & ".png"))
This is the URI of a resource in the Application's assembly:
pack://application:,,,/Resources/[RESOURCENAME]
To get a resource in a DLL, the URI looks more like this:
pack://application:,,,/[DLLNAME];component/Resources/[RESOURCENAME]
For details, see Pack URIs in WPF.

Why cannot load actor image from TheMovieDatabase server even the links works on browser normaly?

This is really weird. This simple code can load any picture from net but not from TheMovieDatabase.
(ActorImage is the normal Image created in xaml)
Code is next:
string ImageUrl = "https://image.tmdb.org/t/p/w500/dRLSoufWtc16F5fliK4ECIVs56p.jpg";
BitmapImage ActorBitmapImage = new BitmapImage(new Uri(ImageUrl, UriKind.RelativeOrAbsolute));
ActorImage.Source = ActorBitmapImage;
Most weird thing is that if I open the browser the link works as it should to work.
Any idea what can cause the problem?

UWP - Image Uri in Application Folder

I'm having a little issue here in showing images.
So when I'm trying to load images from XAML, I can use a relative uri to the image source like this :
<Image Source="/Assets/image.jpg" />
But if I try to change the image source programatically from code behind, I always get an exception and I'm sure it's because of the false Uri. I tried something like this :
BitmapImage bitmapImage = new BitmapImage(new Uri("/Assets/image.jpg"));
Am I doing it wrong? Any help will be appreciated, thanks!
You can also use it with BaseUri.
BitmapImage bitmapImage = new BitmapImage(new Uri(this.BaseUri, "/Assets/image.jpg"));
Assets is a folder name and you can change it with your any custom folder name :)
To access files stored inside the application package, but from code where there is no inferred root authority, you need to specify the ms-appx: scheme :
So in your case it will be something like :
BitmapImage bitmapImage =
new BitmapImage(new Uri("ms-appx:///[project-name]/Assets/image.jpg"));
Read this documentation for more details : https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh965322.aspx
Hope it helps.
I believe WPF is more forgiving and paths like "Assets/image.png" are possible. If you are writing a UWA (win 10) app then you need to use the "ms-appx" URI's.
My experience is "ms-appx:///Assets/image.png" works fine.
Yes, three "/"s (!) and no need for the name of the app in the path.
Image img = new Image();
img.Source = new BitmapImage(new Uri("http://www.contoso.com/images/logo.png"));
Just use it like this
<Image Source="Assets/image.jpg" />

Image doesn't appear when changing its source in Windows Phone

When I want to change an image's source, the image simply disappears.
The code I'm using:
tactImg.Source=tactImgList[i];
tactImgList = new BitmapImage[32];
tactImgList[0] = new BitmapImage(new Uri("ms-appx:///Assets/Images/1.png", UriKind.Absolute));
What am I doing wrong? Is there something I need to add to an xaml file or I'm making the global array wrong?
You don't need to use ms-appx in Windows Phone 8 projects , it's for WinRT projects.
Make sure your image's Build type is Content , remove the ms-appx part and since your image has relative uri , set your UriKind to Relative.
First, fill in the data, and then change the source.
tactImgList = new BitmapImage[32];
tactImgList[0] = new BitmapImage(new Uri("ms-appx:///Assets/Images/1.png", UriKind.Absolute));
tactImg.Source=tactImgList[i];
Or use ObservableCollection for storing images.

Cannot set image source in code behind

There are numerous questions and answers regarding setting image source in code behind, such as this Setting WPF image source in code.
I have followed all these steps and yet not able to set an image. I code in WPF C# in VS2010. I have all my image files under a folder named "Images" and all the image files are set to Copy always. And Build Action is set to Resource, as instructed from documentation.
My code is as follow. I set a dog.png in XAML and change it to cat.png in code behind.
// my XAML
<Image x:Name="imgAnimal" Source="Images/dog.png" />
// my C#
BitmapImage img = new BitmapImage();
img.UriSource = new Uri(#"pack://application:,,,/FooApplication;component/Images/cat.png");
imgAnimal.Source = img;
Then I get a empty, blank image of emptiness. I do not understand why .NET makes setting an image so complicated..w
[EDIT]
So the following works
imgAnimal.Source = new BitmapImage(new Uri(#"pack://application:,,,/FooApplication;component/Images/cat.png"));
It works, but I do not see any difference between the two code. Why the earlier doesn't work and the latter does? To me they are the same..
Try following:
imgAnimal.Source = new BitmapImage(new Uri("/FooApplication;component/Images/cat.png", UriKind.Relative));
Default UriKind is Absolute, you should rather use Relative one.
[EDIT]
Use BeginInit and EndInit:
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(#"pack://application:,,,/FooApplication;component/Images/cat.png");
img.EndInit();
imgAnimal.Source = img;

Categories

Resources