Warm tip: This article is reproduced from stackoverflow.com, please click
c# silverlight visual-studio-2010 windows-phone-7

Setting String as Image Source in C#

发布于 2020-06-07 23:10:35

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?

Questioner
Dan
Viewed
29
thomasmartinsen 2010-12-31 07:26

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;