Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download an image from web showing the progress of the download in c# using winforms?

I download an image from an URL asynchronously using WebRequest this way:

public void Download(string url)
{   
  byte[] buffer = new byte[0x1000];
  WebRequest request = HttpWebRequest.Create(url);
  request.Method = "GET";
  request.ContentType = "image/gif";

  request.BeginGetResponse(result =>
  {
    WebRequest webRequest = result.AsyncState as WebRequest;

    WebResponse response = webRequest.EndGetResponse(result);
    ReadState readState = new ReadState()
    {
      Response = response.GetResponseStream(),
      AccumulatedResponse = new MemoryStream(),
      Buffer = buffer,
    };

    readState.Response.BeginRead(buffer, 0,
      readState.Buffer.Length, ReadCallback, readState);
  }, request);
}

public void ReadCallback(IAsyncResult result)
{
  ReadState readState = result.AsyncState as ReadState;
  int bytesRead = readState.Response.EndRead(result);
  if(bytesRead > 0)
  {
    readState.AccumulatedResponse.BeginWrite(readState.Buffer, 0, bytesRead, writeResult =>
    {
      readState.AccumulatedResponse.EndWrite(writeResult);
      readState.Response.BeginRead(readState.Buffer, 0, readState.Buffer.Length, ReadCallback, readState);
    }, null);
  }
  else
  {
    readState.AccumulatedResponse.Flush();
    readState.Response.Close();
    pictureBox1.Image = Image.FromStream(readState.AccumulatedResponse);
  }
}

public class ReadState
{
  public Stream Response { get; set; }
  public Stream AccumulatedResponse { get; set; }
  public byte[] Buffer { get; set; }
}

and it works ok, but I would like to show the progress of the download as the browsers do, and not to show only when it finishes.

If I do

pictureBox1.Image = Image.FromStream(readState.AccumulatedResponse);

before it finishes I get an exception that the picture is not valid, even though it has some data. Is there anyway to show partial data?

like image 955
Pablo Retyk Avatar asked Apr 27 '09 07:04

Pablo Retyk


People also ask

How do I download progress bar?

In order to make a progressbar for such an application, we will use the tkinter. ttk package that includes the Progressbar module. Initially, we will instantiate an object of Progressbar which has orientation of Horizontal. Then, we will define a function to increase the value of the progressbar and keep updating it.

How do I download an image from a URL?

Click on the Download Image from URL button, the field will appear on the right. Enter the full web address of the image. Click on the arrow to the right of the field and select the Force Check checkbox. Then click the Save button.

How do you make a file downloadable in C#?

Download Files from Web [C#] The simply way how to download file is to use WebClient class and its method DownloadFile. This method has two parameters, first is the url of the file you want to download and the second parameter is path to local disk to which you want to save the file.


2 Answers

JPEG has a special encoding mode called "Progressive JPEG" in which data is compressed in multiple passes of progressively higher detail. Windows 7 has built-in support for this.

like image 168
Anton Gogolev Avatar answered Sep 20 '22 16:09

Anton Gogolev


I would like to show the progress of the download as the browsers do, and not to show only when it finishes.

You have two options:

  1. Use WebClient.DownloadDataAsync. This will raise progress events via DownloadProgressChanged and provide a final notification of when the data is available via the DownloadDataCompleted event. At that point, you could assign the image to, say, the PictureBox.

  2. If you are downloading an image to eventually display in a PictureBox control then it may even be easier to just go with PictureBox.LoadAsync. This too will provide progress updates via its LoadProgressChanged event and finally LoadCompleted on completion.

like image 30
Atif Aziz Avatar answered Sep 19 '22 16:09

Atif Aziz