How to create Uri pointing to an local image in Silverlight? - silverlight

I was wondering if it is allowed to read local images within a Silverlight client. (it is just to test a control)
var image = new BitmapImage { UriSource = new Uri("../Images/2.jpg", UriKind.Relative) };
Would this Uri be allowed and the image created? For some reason the picture doesn't show, despite getting no compiler errors that the Uri might have been wrong...
Thanks for advice,
kave

Assuming your project looks like this
- Project
- Images
- 2.jpg
- Another_Folder
- *your code file*
, that should be fine.
Remember that
../Images/2.jpg
is navigating to parent folder, then looking for an Images folder and a 2.jpg file.

Related

WPF Dispay image from relative location

I would like to display an image on my dialog which could be located in a directory relative to the one the .exe is located in, e.g.
project
- data
-- logo //<-- that's where the image is located
-bin //<-- that's where the .exe is in
A default image should be included in the .exe but on displaying the dialog, the \data\logo directory should be checked first and if an image with the given filename could be found there that one should be used for display instead of the one that is inside the .exe.
Any ideas on how to do this?
Thanks,
tabina
Use the pack URI
pack://application:,,,/ReferencedAssembly;component/data/logo/image.png
As he asks for a folder relative to the exe, i suggest he means the logo should be located inside the filesystem (e.g. c:\program files\MyApp\data\logo). So I would check if the file exists with
File.Exists("/data/logo/logo.png")
If it returns false, you could load your embedded resource.
//Edit
If you use this snippet in your Visual Studio IDE the path is located at
<Project directory>/bin/debug/data/logo
what I am doing now is
// set the logo
string path = Environment.CurrentDirectory + "\\data\\logo\\logo.gif";
var uri = new Uri(path, UriKind.Absolute);
try
{
this.myImg.Source = new BitmapImage(uri);
}
catch (Exception e)
{
this.myImg.Source = new BitmapImage(new Uri(#"pack://application:,,,/myAssemblyName;component/Images/logo.gif"));
}
This works pretty well, but what I don't like about this is that it is all written in the code-behind. I would rather have it more re-usable.
Is there a way to put it all into a template and apply it to viewboxes/images?
Thanks,
tabina

How to access downloaded sound in IsolatedStorage via URI (WP7)?

I basically downloaded a file name custom.mp3 into my isolatedstorage and I can see it via isolatedstorage explorer....
The question here is... How can I access the particular custom.mp3 via URI?
So far I got this.. but I wonder why it is not working:
alarm.Sound = new Uri("isostore:/custom.mp3", UriKind.Absolute);
Your path is wrong. Nothing else is wrong with your code. Post the code you're using for saving the mp3 file in the first place, if you want further help.
For easier reading, the code to store the MP3 goes something like this..
string alarmfile = "custom.mp3";
isolatedStorageFileStream = new IsolatedStorageFileStream(alarmfile,FileMode.Create,isolatedStorageFile);
long songfilelength = (long) e.Result.Length;
byte[] songbyte = new byte[songfilelength];
e.Result.Read(songbyte, 0, songbyte.Length);
isolatedStorageFileStream.Write(songbyte, 0, songbyte.Length);
isolatedStorageFileStream.Flush();
Only files packaged in the XAML can be used as alarm sound:
Remarks
The Sound URI must point to a file packaged in the application’s .xap
file. Isolated storage is not supported. When the alarm is launched,
the sound is played quietly and then gradually increases in volume.
There is no way to modify this behavior.
From:
Alarm.Sound Property

Adding image F#

I have a stack panel to which I want to add some icons dynamically.
If I add a TextBlock to the stack panel, it works perfectly:
// assuming stackPanel is my stack panel
let text = new TextBlock()
text.Text <- "Test"
stackPanel.Children.add(text)
However, my goal is to add an image, but seems like it fails to resolve the image
let getImageSource(imagePath) =
let uri = new Uri(imagePath, UriKind.Relative)
new BitmapImage(uri);
let icon = new Image()
icon.Source <- getImageSource("images/fileIcon/icon.gif")
stackPanel.Children.Add(icon) // this doesnt work
now when I do:
let icon = new Image()
icon.Source <- getImageSource("images/fileIcon/icon.gif")
stackPanel.Children.Add(icon)
let text = new TextBlock()
text.Text <- "Test"
stackPanel.Children.add(text)
I can see there's an empty space between the texts, as if there is an empty image there.
So my guess is there's something wrong with the way I resolve the image path - but I am not sure why.
Thoughts?
Thanks!
In case your gif's Build Action is Resource, then the proper way to address it is /SilverlightApplication1;component/path/to/file.gif. Here SilverlightApplication1 is the name of your silverlight application
In case it's Build Action is Content, then it's proper address is /path/to/file.gif, always with a leading slash when creating a BitmapImage.
Check out Silverlight 2: Demystifying URI references for app resources for more information.
For easier debugging of image loading problems, hook to the BitmapImage.ImageFailed event and see what kind of errors crop up.
One last note, AFAIK Silverlight doesn't support the GIF format. You might use PNG instead.
You can try with the following Uri if yours is WPF App.
let uri = Uri("pack://application:,,,/asm_name;component/images/fileIcon/icon.gif")
asm_name have to be replaced with your actual assembly name.
if your are working on Silverlight application, you need to modify the uri like this. Assuming that the build action of icon.gif is Resource.
let uri = Uri("../images/fileIcon/icon.gif", UriKind.Relative)
Hope this helps.

Loading BitmapImage in code

From my assembly (A) I want to call a method in another assembly (B) which passes an image. This image is then shown in a WPF Window - the window is part of B's project.
I can't seem to pass an ImageSource with a pack:// uri as this gets evaluated in the context of B, so I guess I need to cache the image using CachedBitmap (?) when still in A.
BitmapImage img = new BitmapImage(new Uri("Images/32px-Nuvola_apps_cache.png", UriKind.Relative));
CachedBitmap cbmp = new CachedBitmap(img, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
I've managed to get this to work if I set the image to Embedded Resource and load it as a stream, but this isn't the WPF way.
It seems from the pack: documentation that I should be able to do this, but I've tried these below and none work;
"Images/32px-Nuvola_apps_cache.png": "Could not find part of the path"
"pack://application:,,,Images/32px-Nuvola_apps_cache.png": "The URI prefix is not recognized.".
"pack://siteoforigin:,,,Images/32px-Nuvola_apps_cache.png": "The URI prefix is not recognized."
All I want to do is load a Resource .png file into memory and pass it wholesale to a method in another assembly.
Thanks
Paul.
Try:
pack://application:,,,/YourAssemblyName;component/Images/32px-Nuvola_apps_cache.png

Silverlight image: load URL dynamically?

I'm tinkering with Silverlight 2.0.
I have some images, which I currently have a static URL for the image source.
Is there a way to dynamically load the image from a URL path for the site that is hosting the control?
Alternatively, a configuration setting, stored in a single place, that holds the base path for the URL, so that each image only holds the filename?
From what I gather you aren't trying to change the image itself dynamically, but rather to correctly determine the location of the image at runtime.
I believe simply prefixing the image relative URL with "../" should get you to the root of your application, not necessarily the site as the application might not be hosted in the root of a site.
If your XAP file is located as follows:
http://somesite.foo/app1/somethingelse/clientbin/MyFoo.xap
And you where trying to link the following image:
http://somesite.foo/app1/somethingelse/images/a/boo.png
Apparently all relative URI's are relative to where the XAP file is located (ClientBin folder typically) and Silverlight appends the current Silverlight client namespace. So if you Silverlight control is in the namespace Whoppa you would need to put all your images in the clientbin/Whoppa/ directory. Not exactly convenient.
The workaround is to use absolute URIs as follows:
new Uri(App.Current.Host.Source, "../images/a/boo.png");
In the code behind or a value converter you can do
Uri uri = new Uri("http://testsvr.com/hello.jpg");
YourImage.Source = new BitmapImage(uri);
// create a new image
Image image = new Image();
// better to keep this in a global config singleton
string hostName = Application.Current.Host.Source.Host;
if (Application.Current.Host.Source.Port != 80)
hostName += ":" + Application.Current.Host.Source.Port;
// set the image source
image.Source = new BitmapImage(new Uri("http://" + hostName + "/cute_kitten112.jpg", UriKind.Absolute));
http://www.silverlightexamples.net/post/How-to-Get-Files-From-Resources-in-Silverlight-20.aspx
using System.Windows.Resources; // StreamResourceInfo
using System.Windows.Media.Imaging; // BitmapImage
....
StreamResourceInfo sr = Application.GetResourceStream(new Uri("SilverlightApplication1;component/MyImage.png", UriKind.Relative));
BitmapImage bmp = new BitmapImage();
bmp.SetSource(sr.Stream);
SilverlightHost.Source will provide you the URL that was used to load the XAP file. You can use this to then construct a relative URL for your images.
So if for example your XAP is hosted on http://foo.bar/ClientBin/bas.xap and your images were stored in http://foo.bar/Images/ you can simply use the Source to grab the host name and protocol to construct the new URI.
img.Source = new BitmapImage(image uri) must work.
img.Source = new BitmapImage(new Uri("/images/my-image.jpg", UriKind.Relative)); will properly resolve to the root of the Silverlight application where as "../images/my-image.jpg" will not.
This is only true in the code-behind when dynamically setting the source of the image. You cannot use this notation (the "/" to designate the root) in the XAML (go fiquire, hope they fix that)
The below code worked for me only when the image is included in the project as a resource file:
img.Source = new BitmapImage(new Uri("/images/my-image.jpg", UriKind.Relative));
I am unable to access URL from absolute URLs. Not even Flickr's farm URL for images.

Resources