0

In my XAML, I have:

<Image Height="150" HorizontalAlignment="Left" Margin="0,4,0,0" Name="imgLogo" Stretch="Fill" VerticalAlignment="Top" Width="417" />
<Image Height="343" HorizontalAlignment="Left" Margin="0,155,0,0" Name="imgPhoto" Stretch="Fill" VerticalAlignment="Top" Width="417" />

In the C# code behind, I have:

WebClient wcForLogo = new WebClient();
wcForLogo.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wcForLogo_DownloadStringCompleted);
wcForLogo.DownloadStringAsync(new Uri("http://mySite/logo.gif"));

WebClient wcForPhoto = new WebClient();
wcForPhoto.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wcForPhoto_DownloadStringCompleted);
wcForPhoto.DownloadStringAsync(new Uri("http://mySite/photo.jpg"));

But now I don't know how to catch the image and post it in the XAML controls I built.

2 questions:

  1. Is there a way to copy the e.Result directly to the Image controls, or should I cache the image and use the cache as the source, or should I save the image to the isolated storage and then use that as the source, then delete the image when I'm done with it? Whichever case, could you please show me how with code?
  2. Are GIF's and JPG's dealt with differently? If so, can you show me the 2 different ways?
Ali Almohsen
  • 1,311
  • 3
  • 13
  • 24

4 Answers4

2

If you just want to display the picture, you don't have to use a WebClient. You can set the Uri directly in the image source, and the control will take care of the downloading:

imgLogo.Source = new BitmapImage(new Uri("images/yourPicture.png", UriKind.Relative));

Note that GIF aren't supported by the Image control. You can still display them by using the converter from the ImageTools library: Display GIF in a WP7 application with Silverlight

Community
  • 1
  • 1
Kevin Gosse
  • 38,392
  • 3
  • 78
  • 94
  • Thanks a lot for the link! That turned out to be the best answer as I'm not sure what image format the images will come in, so I'm following the advice of one of the blog posts your linked which suggested using a browser control instead. – Ali Almohsen Apr 24 '12 at 11:35
1
using System.Net;
using System.IO;
        private void Form1_Load(object sender, EventArgs e)
        {
            WebClient webclient = new WebClient();
            webclient.DownloadDataAsync(new Uri("http://mySite/logo.gif"));
            webclient.DownloadDataCompleted += callback;

        }
        void callback(object sender,DownloadDataCompletedEventArgs e)
        {
            var ms = new MemoryStream(e.Result);
            pictureBox1.Image = Image.FromStream(ms);
        }
  • Thanks for the response, but the others' answers were much more wholesome. I'd love to +1 your post for a correct answer anyways, but I don't have the rep to do so. Thanks though. :D – Ali Almohsen Apr 24 '12 at 11:33
0

If you want to save in isolated storage use like this

            WebClient m_webClient = new WebClient();

            Uri m_uri = new Uri("http://URL");

            m_webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);

            m_webClient.OpenReadAsync(m_uri);




        }



    void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        int count;

        Stream stream = e.Result;

        byte[] buffer = new byte[1024];


        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        {




            using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("IMAGES.jpg", FileMode.Create, isf))
            {
                count = 0;

                while (0 < (count = stream.Read(buffer, 0, buffer.Length)))
                {
                    isfs.Write(buffer, 0, count);
                }

                stream.Close();
                isfs.Close();
            }
        }

To get image form isostore:

byte[] data;

        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        {

            using (IsolatedStorageFileStream isfs = isf.OpenFile(uri, FileMode.Open, FileAccess.Read))
            {
                data = new byte[isfs.Length];
                isfs.Read(data, 0, data.Length);
                isfs.Close();
            }

        }


        MemoryStream ms = new MemoryStream(data);

        BitmapImage bi = new BitmapImage();

        bi.SetSource(ms);

If you give the image name as image then set the source as bi:

        image.source = bi;

If you want to add directly

  WebClient client = new WebClient();
  Stream stream = client.OpenRead(imageUrl);
  Bitmap bitmap = new Bitmap(stream);
  image.source = bitmap;
SENTHIL KUMAR
  • 647
  • 4
  • 17
  • Thanks for the excellent response! This'll be a great reference to go back to for IsolatedStorage as well, but I decided to go with the simpler solution since I'd rather not save the image. I'd love to +1 your post, but I don't have the rep to do so. Thanks though. :D – Ali Almohsen Apr 24 '12 at 11:31
  • What answer you expect you asked here fetch image from the web url but you mark as answer the reply for getting image from local content. It is not good – SENTHIL KUMAR Apr 24 '12 at 11:34
  • If you not asked for web url means then y ask question like this man change the format of your question while asking – SENTHIL KUMAR Apr 24 '12 at 11:36
  • @SENTHILKUMAR, the answer that Ali marked is not just for "getting image from local content" as you state; it also works for remote images as he required in his question! – Pedro Lamas Apr 24 '12 at 11:38
  • for remote we should add urikind.absolute right anyhow he accepts no probs thanks for your valuable reply to him – SENTHIL KUMAR Apr 24 '12 at 11:43
0

Let's say your image control is called MyImage, you can just do this to load an image from an URL:

MyImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://mySite/photo.jpg"));

No need to do all the plumbing just to download the image, the framework already does that for you!

Pedro Lamas
  • 7,185
  • 4
  • 27
  • 35
  • Your solution would've worked more perfectly for me than the chosen answer, but he added the link about dealing with GIF's, which is a big deal. I'd love to +1 your post, but I don't have the rep to do so. Thanks though. :D – Ali Almohsen Apr 24 '12 at 11:30