Access images in Windows Phone Class Library - c#

I want to access some images in my Windows Phone class library, but how can I access these images?
I have a method which will set the image, but
new Uri("/Assets/Images/MyImage.png", UriKind.Relative);
doesn't work. I've tried setting the image to Resource instead of Content, but it also doesn't work.
So how can I provice images in my class library?

From what I gather it looks like you are trying to set the source of an image from the XAML code behind file.
If so you can do so as follows:
MyImage.Source = new BitmapImage(new Uri("/Assets/Images/MyImage.png", UriKind.Relative));
Hope this helps.

Related

Xamarin Forms XAML - Unable to create multiple images from a list of URLs

I have a List where the string is the url of an image.
I am trying to dynamically create a grid of images with the source being each url.
I have tried binding but it doesn't seem to work. I also cannot access the x:Name of an image I created in Xaml. I am open to doing this in Xaml or in the code behind.
Any ideas of how to do this.
//imageList is a List<string> with your image urls in it
foreach(var i in imageList) {
Image image = new Image();
image.Source = ImageSource.FromUri(new Uri(i));
// you will need to add logic to calculate the Row/Column for each image
myGrid.Children.Add(image,x,y);
}

Relative Resource Uri in WPF Forms

I'm looking for a method to make all my resource files work properly after I have written my project in a cd.
For example I have saved my images in Project Name\Project Name\obj\Debug\Images and I want them to be usable both in xaml and regular c# code, when I insert that cd in another machine.
Thanks for your time!
What worked for me was, using the Image class for both backgrounds and overlapping images.
In xaml the code is <Image x:Name="Image1" Source="..\obj\Debug\Images\image.jpg"/>
and you initialise it in c# using
public PageName()
{
InitializeComponent();
Image1.Source = new BitmapImage(new Uri(#"../Images/cafeteria.jpg", UriKind.Relative));
}
It's a shame because (atleast in my case) the ImageBrush Class didn't support relative uris for some reason..

C#/XAML - Assets not shown when modified by code

I created a user control, which is made with images.
I tried to modify them by C# method, for example:
imgElement.Source = new BitmapImage(new Uri("ms:appx/Resources/IconElement/my_icon_element.png"));
imgBackgroundElement.ImageSource = new BitmapImage(new Uri("ms:appx/Resources/BackgroundElement/my_background_element.png"));
The soft finds images but doesn't show them ; the image control is empty.
If do not use the method and let images by default, they are shown...
Can anyone teach me why ? ^^
Thanks !
An absolute path for your image will be:
ms-appx:///Resources/IconElement/my_icon_element.png

DirectoryNotFoundException when creating a BitmapImage

I develop an IOC framework in the form of a DLL that creates the application's main window and displays various WPF Pages in it. There is a folder of PNG images with their Build Action set to Resource. I use code like this to set the sources of WPF Image elements...
MyImage.Source = new BitmapImage(new Uri("/MyAssembly;component/Images/MyImage.png", UriKind.Relative));
This works great. However, there is also a feature where the user can choose to display one of these WPF Pages in a separate window. Here is the mystery: If the user navigates to the page in the main window before showing it in a separate window, all is well. However, if the user shows the page in a separate window before it is shown in the main window, I get a DirectoryNotFoundException saying " Could not find a part of the path 'C:\MyAssembly;component\Images\MyImage.png'."
I discovered the OnLoad cache option and tried the following code...
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri("/MyAssembly;component/Images/MyImage.png", UriKind.Relative);
image.EndInit();
MyImage.Source = image;
However, I then get the DirectoryNotFoundException on the very first image that I attempt to create.
I also tried an absolute URI...
MyImage.Source = new BitmapImage(new Uri("pack://application:,,,/MyAssembly;component/Images/MyImage.png", UriKind.Absolute));
I then get a UriFormatException saying "Invalid URI: Invalid port specified."
I solved the problem by using the 'pack' URI format as shown in the question. However, I needed to make a call to the Application class before I did so. This has the effect of executing the Application's static constructor which registers the 'pack' URI scheme. I found my answer here. The call I used was simply this:
var app = Application.Current;

How to use gif animated image in WP 7

I have seen this post: Display GIF in a WP7 application with Silverlight
But in my case? for animating I am using a popup. So when application starts it shows a popup for 5 seconds. In this popup I want to show some .gif image, but it doesn't work.
Here is the code which I implement:
public partial class AnimatedSplashScreen : UserControl
{
protected Uri ImageSource
{
get;
set;
}
public AnimatedSplashScreen()
{
InitializeComponent();
ImageSource =
new Uri(
"http://upload.wikimedia.org/wikipedia/commons/thumb/3/36/Sunflower_as_GIF.gif/200px-Sunflower_as_GIF.gif",
UriKind.Absolute);
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
}
And xaml code is:
<UserControl.Resources>
<imagetools:ImageConverter x:Key="ImageConverter" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot"
Width="480"
Height="800"
Background="White">
<imagetools:AnimatedImage Source="{Binding ImageSource, Converter={StaticResource ImageConverter}}" />
But in result it does't work, it shows an empty background.
Updated:
ImageTools.IO.Decoders.AddDecoder();
ImageSource = new Uri("http://a3.twimg.com/profile_images/1136683647/hisoka_normal.gif", UriKind.Absolute);
It still doesn't work
Finally working... Talk about events conspiring against you... You need to fix all these things first!
(note there is a following problem with only the first 2 frames being animated but that is for another question):
Part 6 (getting sleepy now)
Lastly relative image URLs starting with "/" are not supported by the ImageTools.Controls.ImageConverter, so you need to use a relative URL without the leading "/". I found that once every other problem was fixed I was getting an unsupported exception with the path.
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
InitializeComponent();
this.ImageSource = new Uri("layer1.gif", UriKind.Relative);
this.DataContext = this;
Part 5
You need to set the binding DataContext somewhere.
You do not connect the XAML page DataContext to the code behind object. I could not see where you had done this. A very simple/quick way is to set this.DataContext = this; in the page's constructor.
Part 4
You can only bind to public properties!
Your ImageSource property is currently protected. Change it to Public
public Uri ImageSource
{
get;
set;
}
Part 3
I also note your ImageSource property is not an INotifyPropertyChange type property. So setting it after InitializeComponent will not work.
Try it this way round (or change it to use a notify property):
public AnimatedSplashScreen()
{
ImageSource =
new Uri(
"/200px-Sunflower_as_GIF.gif",
UriKind.Relative);
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
InitializeComponent();
}
Part 2 (actually not support by ImageTools.Controls.ImageConverter)
The cross domain file was apparently only one problem. Based on the comments you also need to store your images on your own website and reference them with an appropriate URI format.
If you put your files in a folder called images under ClientBin you use this format:
"/images/imagename.jpg"
This is the best option as the images also use Browser caching!
For your example it would be like this:
ImageSource =
new Uri(
"/images/200px-Sunflower_as_GIF.gif",
UriKind.Relative);
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
and put the example file in your client bin folder under images.
If you do not use the leading "/" Silverlight assumes the files are resources in the current module instead e.g.
"images/imagename.jpg"
Part 1
This is actually a copyright issue, to stop people deep-linking files from other people's sites without permission.
The Wikimedia.org site does not have any cross domain access files e.g.:
http://upload.wikimedia.org/crossdomain.xml
http://upload.wikimedia.org/crossdomainpolicy.xml
wikimedia.org/crossdomain.xml
wikimedia.org/crossdomainpolicy.xml
... presumably as they do not want others to use the files they host there outside of their own website.
That means Silverlight will not allow access to files on those sites as it is a good Internet citizen. Try hosting the files on your own site (where your Silverlight app resides), then it will not need any cross domain access file at all.
Side note: If you do ever need a cross domain file on a website, for use by Silverlight, use a crossdomainpolicy.xml as the other one is not as useful (designed for older flash use)

Categories