Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting String as Image Source in C#

Well I've looked up several methods to fix this in my Windows Phone 7 app but I can't seem to find anything that works. What confuses me is that I've done something just like this before with no problem, so I'm not sure why it's not working. The code causing me the problem is this:
if (appSettings.Contains("image")) myImage.Source = (string)appSettings["image"]; else myImage.Source = "default.jpg";

The error I get is this

Cannot implicitly convert type 'string' to 'System.Windows.Media.ImageSource.

The reason this confuses me is because I did this Twitter app tutorial, in which you bind the image source directly to a string. So what can I do to fix this?

like image 832
Dan Avatar asked Dec 30 '10 23:12

Dan


2 Answers

You need to specify an ImageSource instead of a string when doing it from code:

Uri uri = new Uri("...", UriKind.Absolute); 
ImageSource imgSource = new BitmapImage(uri); 
myImage.Source = imgSource; 
like image 66
thomasmartinsen Avatar answered Nov 20 '22 17:11

thomasmartinsen


Unless I've not found the right part of Scott's post that you're looking at, he is binding image source to a url.

Specificaly,

ImageSource = tweet.Element("user").Element("profile_image_url").Value

Which would be something like

http://a0.twimg.com/profile_images/1158022545/mickn_normal.jpeg

like image 2
Mick N Avatar answered Nov 20 '22 18:11

Mick N