1

I download image with below code.

    byte[] buffer = new byte[1024];

    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
    httpRequest.Timeout = 30000;
    httpRequest.Method = "GET";
    httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0";
    httpRequest.Accept = "image/png,image/*;q=0.8,*/*;q=0.5";

    using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
    {
        using (Stream responseStream = httpResponse.GetResponseStream())
        {
            MemoryStream memStream = new MemoryStream();
            int bytesRead;
            while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                memStream.Write(buffer, 0, bytesRead);
            }
            memStream.Seek(0, SeekOrigin.Begin);
            return memStream;
        }
    }

after download, Show it in WPF Image control.

    BitmapImage bmpImage = new BitmapImage();
    bmpImage.BeginInit();
    bmpImage.StreamSource = memStream;
    bmpImage.CacheOption = BitmapCacheOption.OnLoad;
    bmpImage.EndInit();

    backIMG.Source = bmpImage;

I want show image while downloading (similar image loading in browsers) How can I do this?

(excuse me for bad english)

g2sea
  • 95
  • 1
  • 8
  • in wpf you can directly bind byte[] to source property of image. if you are getting data from webrequest as byte[]. just do like . < – JSJ May 18 '12 at 12:48
  • thanks, but doesn't work for my case. I need show slices of image during downloading. – g2sea May 18 '12 at 13:54
  • 1
    This may help: http://stackoverflow.com/questions/6000212/how-can-i-display-a-progressive-jpeg-in-wpf – Josh May 18 '12 at 16:58
  • Very thanks. this link solve my problem. – g2sea May 18 '12 at 19:10

0 Answers0