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.
Related
I use below code to load image from URL in my winodws phone 7 application.
Uri uri = new Uri("http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif", UriKind.Absolute)
image1.Source = new BitmapImage(uri);
It is working fine for me. But image is loading asynchronously and by the time I want to show some kind of busy indicator there and if image does not exist on such URL then I want to show some default image. How can I achieve that?
I think if you are subscribed to the Image.ImageFailed Event you should be able to show to default image in case of a non-existing image.
Conditions in which this event can occur include the following:
File not found.
Invalid (unrecognized or unsupported) file format.
Unknown file format decoding error after upload.
So something like this might work for you:
image1.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(handlerImageFailed);
Uri uri = new Uri("http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif", UriKind.Absolute)
image1.Source = new BitmapImage(uri);
void handlerImageFailed(object sender, ExceptionRoutedEventArgs e)
{
// Show the default image
}
In a mobile application I try to load an image from a sqlite database and want to show it in a mxml image component.
The loading of the bytearray works fine, but when I assign the bytearray to the image component I get following error.
Error #2044: Unhandled securityError:. text=Error #3226: Cannot import
a SWF file when LoaderContext.allowCodeImport is false.
I also tried to save and load the image as a base64 string. but it does not help.
Even if I try a simple thing like this:
var byteArray:ByteArray = img1.loaderInfo.bytes;
img2.source = byteArray;
just add the bytearray of img1 to the empty img2 - the same error occurs.
whats going wrong here?
many thanks for your help,
cheers,
Flo
I don't understand why it thinks you're loading swf bytes.
Try this little hack just for kicks and tell me what happens :
var bitmapData : BitmapData = new BitmapData(content.width,content.height,true,0x00000000);
bitmapData.draw(loader.content, new Matrix(),null,null,null,true);
img2.source = new Bitmap( bitmapData );
where loader is your loader obviously.
Are you using mx or spark Image?
mx:Image extends SWFLoader, so trying to set its source directly could result in a security error because you are essentially importing code. spark:Image wraps a BitmapImage. You will likely have better luck with that.
Edit I just saw your comment. I've also had security issues with loading bytearray data and setting it to the source of BitmapImage. Mine were sandbox issues due to no crossdomain.xml, but I've never used the mobile sdk.
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.
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
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.