Surface Pro 7 deal! Save big at Amazon right now
- Hi, I want to use a remote URL as an Image source. I have tried different things, but noting worked -.-.
My code looks like this:
<Grid Height="500" HorizontalAlignment="Left" Margin="0,0,0,0" Name="grid1" VerticalAlignment="Top" Width="470" Background="{x:Null}">
<Image Height="109" HorizontalAlignment="Left" Margin="240,19,0,0" Name="image2" Stretch="Fill" VerticalAlignment="Top" Width="180" MaxHeight="50" />
</Grid>
Now I want to add the image source like this:
var imgurl = "http://www.blab/blabla.jpg";
image2.Source = new BitmapImage(new Uri(imgurl));
The problem is, this does not work -.-
Has anyone an idea what is wrong? I'm trying the last two nights, but I can't find the mistake10-04-2012 04:28 PMLike 0 - You need to use the webclient to Download the image asynchronously and set the source as the stream you get back.10-05-2012 08:30 PMLike 0
- Hi Panathas,
Your code looks fine to me (it should work the way you are doing it). Normally I'd say you should specify the UriKind when specifying the Uri, i.e.:
Code:new Uri(imgurl, UriKind.Absolute)
Are you sure some other part of your UI/layout/something else on the page is not interfering with the display of the items? It may be worth you creating a new simple project in visual studio without the app doing anything other than just showing a picture on the page.
Do you definitely have an internet connection? If you are using the emulator to test this are you able to browse to the image using IE on the emulator (just to check the internet connection is working, and nothing is being blocked e.g. by your firewall)?
Where are you putting the code to do this? E.g. Are you sure you are on the same 'thread' as the UI? (you can't update interface elements across threads without some extra code)
One thing that could help you debug - you can get it to call a method if the image load fails which may tell you what the problem is (if you set a breakpoint on the method it calls and examine the exception) - e.g.:
Code:image2.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(image2_ImageFailed); image2.Source = new BitmapImage(new Uri(imgurl, UriKind.Absolute)); } void image2_ImageFailed(object sender, ExceptionRoutedEventArgs e) { // set a breakpoint on this method and examine 'e.ErrorException' if the breakpoint is hit }
Code:image2.ImageOpened += new EventHandler<RoutedEventArgs>(image2_ImageOpened);
Good luck with your app.
IanLast edited by Red Lemur; 10-06-2012 at 07:57 PM.
10-06-2012 07:50 PMLike 0 - Thank you very much, both :)
I think I have found the problem with your help. It was not the code, but it is the webpage -.-
When I try a URL from an image of another page it works, but when I try from this page, it does not work. I am not sure why, the URI of the images seem to be ok, e.g. this is one what does not work: http://www.defencenet.gr/defence/ima...ies/tragas.jpg
Getting the text of this page is no problem, but images do not work, weird.
Update: When I download a image of the page and set it manually, it works. My code is correct, I have an internet connection. So what is the problem??? Is it possible to download an image using an other way and set it then?Last edited by Panathas; 10-09-2012 at 01:46 PM.
10-09-2012 04:19 AMLike 0 - Thank you very much, both :)
I think I have found the problem with your help. It was not the code, but it is the webpage -.-
When I try a URL from an image of another page it works, but when I try from this page, it does not work. I am not sure why, the URI of the images seem to be ok, e.g. this is one what does not work: http://www.defencenet.gr/defence/ima...ies/tragas.jpg
Getting the text of this page is no problem, but images do not work, weird.
Update: When I download a image of the page and set it manually, it works. My code is correct, I have an internet connection. So what is the problem??? Is it possible to download an image using an other way and set it then?
E.g. I might use hotlink protection on my photography site to stop some people directly downloading my photos (without my permission) but they still display when someone loads the web page that uses them.
Hope that explains it.10-09-2012 03:15 PMLike 0 -
- Another problem you may need to consider is that some sites will return an HTTP Response of 200 and yet serve up a different image (e.g. an image with the text "hotlinking is not allowed").
To ultimately get around this you may need to first fetch the image remotely and store it on your server (you can do this when you have the ability to set the referrer of the HTTP request), and then have the phone reference it from your server.
Typically, the reasons for pursuing an option like this are shady at best, and apps that have to resort to these options don't exactly have the best success in the marketplace. This is just food for thought. Good luck with your app, I'd be happy to review it if you feel the need.
-kw10-17-2012 11:36 PMLike 0
- Forum
- Developers
- Developers Corner
How to use remote URL for an image?
LINK TO POST COPIED TO CLIPBOARD