Setting String as Image Source in C#

Posted by Dan on Stack Overflow See other posts from Stack Overflow or by Dan
Published on 2010-12-30T23:18:21Z Indexed on 2010/12/30 23:54 UTC
Read the original article Hit count: 239

UPDATE: Okay I've changed my code to this:

            if (appSettings.Contains("image"))
        {
            Uri uri = new Uri( (string)appSettings["image"] + ".jpg", UriKind.Absolute);
            ImageSource imgSource = new BitmapImage(uri);
            myImage.Source = imgSource;
        }
        else
        {
            Uri uriDefault = new Uri("default.jpg", UriKind.Absolute);
            ImageSource imgSourceDefault = new BitmapImage(uriDefault);
            myImage.Source = imgSourceDefault; 
        }

But now I get "Invalid URI: The format of the URI could not be determined".


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: http://weblogs.asp.net/scottgu/archive/2010/03/18/building-a-windows-phone-7-twitter-application-using-silverlight.aspx , in which you bind the image source directly to a string. So what can I do to remedy this?

© Stack Overflow or respective owner

Related posts about c#

Related posts about Silverlight