ForeverLearning
ForeverLearning

Reputation: 1037

Get image from Webpage and show (Windows Phone)

I'm having abit of trouble getting an image from a website and showing it on Windows Phone. The image link changes every day (http://apod.nasa.gov/apod/astropix.html) but the website link is the same.

I've seen that many people use HTMLAgility pack, but I don't think I can use it for Windows Phone.

How would I download an image from a webpage and show it in a imageview/picturebox?

I'm assuming it would go something like Parse Website > Get src string > load string > load image > show image. But would really like some help regarding how to do it.

Thanks!

Upvotes: 0

Views: 402

Answers (1)

Anobik
Anobik

Reputation: 4899

Hi Here's a code block to help. I have not followed the best practices of writing it down still :

    void ImageDownloader() 
    {
        WebClient client = new WebClient();
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        client.DownloadStringAsync(new Uri("http://apod.nasa.gov/apod/astropix.html"));

    }

    void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        string str = e.Result.Remove(0,(e.Result.IndexOf("SRC=")+5));
        str = "http://apod.nasa.gov/apod/"+str.Substring(0, (str.IndexOf(".jpg")+4));
        ImageBrush imb = new ImageBrush();
        imb.ImageSource = new BitmapImage(new Uri(str));
        LayoutRoot.Background = imb;
    }

Home th e string "http://apod.nasa.gov/apod/" remains constant.

Here LayoutRoot is the main grid. U can use an imageview or a picture box as per ur convinience.

Upvotes: 1

Related Questions