WinRT: Image - image doesn't show up if not included as resource - wpf

My Windows Store app/Metro/Win RT app downloads image from server on to some local folder. I need to bind Image control to the downloaded image at runtime.
The problem is that image doesn't show up unless I add it as a binary resource to the project.
My downloaded images are stored in ProjectFolder/Data/Media. Now, here is how I bind image source to image control.
<Image x:Name="WriterImage" Stretch="None" Source="{Binding Path=PersonData.Photo.MediaImageSource"></Image>
public ImageSource MediaImageSource
{
// Here _MediaUrl gets a value: ms-appx:///Data/Media/Writer1.jpg
BitmapImage source = new BitmapImage(new Uri(_MediaUrl));
}
This works only if I add Writer1.jpg as a resource to the project. If I remove it from the project, it doesn't show up.

Note that there are different URL schemes that your application can access:
ms-appx:/// is a read-only location, and refers to files that are included (compiled) with your application, such as resources.
ms-appdata:///local/ refers to local read-write storage for your application. If you are downloading files, my guess is that you should use this URL scheme.
For your example above, I would try using the following URL:
ms-appdata:///local/Data/Media/Writer1.jpg
See the following for more info about URL (URI) schemes: http://msdn.microsoft.com/en-us/library/windows/apps/jj655406.aspx

Related

Accessing folder in wpf app

I have a wpf app with this folder structure:
In the HomeController, I want to access the Images folder. I want to store an image in the Images folder from the HomeController. How can I do this? I tried using pack origin but it takes me to bin.
This should do the trick:
pack://application:,,,/Images/image.png
Can be used in xaml or in code. For more detailed explanation you can review this Microsoft page about it.
In code: Uri uri = new Uri("pack://application:,,,/Images/image.png");
In xaml: <element attribute="pack://application:,,,/Images/image.png"/>

WPF doesn't load images correctly

I have some icons in my WPF project to make the application more user friendly. WPF shows all icons on my computer but when I run the app on other computers it will show some of icons but not all of them. How can I fix it?
Update:
<Image Source="Office/Add-Female-User.ico" Stretch="None" />
I'm sure that all images are accessible because some of them are visible.
You are referencing your images (in your image controls) as Content not Resource. I suspect the problem is your images aren't copied to the output dir. View properties on missing image files and set build action to Content and Copy to output dir to Copy if newer.
For resource referencing see http://msdn.microsoft.com/en-us/library/aa970069.aspx.

WP7 set Image to source behind a HttpBasicAuthenticator

i am trying to set an image in a Windows Phone 7 App, there is a problem thoug, the url where the image is located required Authenticator, i have a valid username and password, but i have no idea how to load the image thoug an URI with the Authenticator?
ProfileImage.Source = new BitmapImage(new Uri(userObject.ProfileImage));
Unless you can use a authentication url, ie. http://username:password#domain.tld/image.png then you need to download the image locally first, and then load it afterwards.
You could write a custom handler for it, that downloads it to the Isolated Storage, and then automatically updates the UI, while using a fallback image (typically blank) while loading it.

How to use relative path of Silverlight app in image control inside user control?

I created a custom user control which contains an <Image /> control. My user control is bound to a CLR object which contains the proper filename to use for the <Image> Source property.
The user control is located under Controls folder in my Silverlight app. When the app runs, the image is only displayed if the image is in the same folder as the user control (i.e. the image must in the Controls folder).
How can I make the image source be relative to the location the Silverlight app is running?
The app is hosted in an ASP.NET MVC application.
A relative path that starts with "/" is considered be routed at the top level of the XAP file and (if the resource is not found in the Xap) in the server folder from which the XAP was downloaded.
In other words place a "/" at the beginning or your Urls and you will probably get the behaviour you describe.

Pointing to an image on Server within XAML

I can do this within code to load a bitmapimage located on the Server under ClientBin\Images folder:
var image = new BitmapImage(new Uri(Application.Current.Host.Source, "./Images/Default.JPG"));
However how do I do this within XAML itself? Is it even possible?
<Image x:Name="NewImage" Source="../Images/Default.JPG"/>
Many Thanks,
Did you try this:-
<Image x:Name="NewImage" Source="/Images/Default.JPG"/>
Urls in Xaml treat the folder from which the Xap is downloaded (ClientBin usually) as the root, i.e. the path "/" points actually at ClientBin.
You can't use a relative path in silverlight for an image on the server since the xap file is downloaded to the client, so the application is not actually running on the server.
you will have to use the full "http://mysite.com/myImage.jpg" path

Resources