4

In a WPF project, I use the method

webBrowser.Navigate(strUrl);

to get a PNG picture from the server.

The following dialog appears:

File Download dialog screenshot

How could I download the picture seamlessly (without a dialog)?

Toodoo
  • 8,570
  • 6
  • 35
  • 58
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
  • 2
    Since its a wpf application, don't save files via webbrowser. you can write to you drive location with a filestream instead. – Henrik Bøgelund Lavstsen Apr 03 '19 at 09:23
  • And if hte file is on a remove server, you can request download the stream, then move it down to your filesteam to the drive. Currently you just open pictures with the browser, and the browser will do its default task. – Henrik Bøgelund Lavstsen Apr 03 '19 at 09:25
  • You should preform some tricks for that, check this [thread](https://stackoverflow.com/questions/77659/blocking-dialogs-in-net-webbrowser-control) – Pavel Anikhouski Apr 03 '19 at 14:49
  • 2
    This is the combination of server information (the "Content-Disposition" header can be set to `inline` or `attachment`) and HTTP user agent (here IE embedded in WPF) configuration. See BrowserFlags registry settings https://support.microsoft.com/en-us/help/982995/a-new-application-window-opens-when-you-try-to-view-an-office-2010-or and https://learn.microsoft.com/en-us/windows/desktop/api/shlwapi/ne-shlwapi-filetypeattributeflags – Simon Mourier Apr 08 '19 at 10:12
  • Did my answer work for you, or is there a reason you need to use the webBrowser control? – Neil B Apr 11 '19 at 23:12
  • @NeilB I need to use the WebBrowser control to show the picture after it has been downloaded - it is comfortable because this control can manage the both things :) – Leo Chapiro Apr 12 '19 at 10:11
  • OK I added an example for that using an image control. – Neil B Apr 12 '19 at 19:30
  • Did any of my examples work for you? – Neil B Apr 15 '19 at 10:16
  • @NeilB Thank you, I'll try it. – Leo Chapiro Apr 15 '19 at 13:30

1 Answers1

3

You don't need to use a browser control for this.

Try using DownloadFileAsync()

Here is a fully working sample. (Change paths as needed.)

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        WebClient client = new WebClient();
        client.DownloadFileAsync(new Uri("https://www.example.com/filepath"), @"C:\Users\currentuser\Desktop\Test.png");
        client.DownloadFileCompleted += Client_DownloadFileCompleted;
        client.DownloadProgressChanged += Client_DownloadProgressChanged;
    }

    private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        progressBar.Value = e.ProgressPercentage;
        TBStatus.Text = e.ProgressPercentage + "% " + e.BytesReceived + " of " + e.TotalBytesToReceive + " received.";
    }

    private void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        MessageBox.Show("Download Completed");
    }

You can open the downloaded file with the default app like this:

System.Diagnostics.Process.Start(@"C:\Users\currentuser\Desktop\Test.png");

EDIT:

If your goal is simply to display the png, you could download it to a stream, and then display it in an image control.

Fully working sample.

WebClient wc = new WebClient();
MemoryStream stream = new MemoryStream(wc.DownloadData("https://www.dropbox.com/s/l3maq8j3yzciedw/App%20in%205%20minutes.PNG?raw=1"));
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(stream);
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = stream;
bi.EndInit();
image1.Source = bi;

Here is the async version.

private void Button_Click(object sender, RoutedEventArgs e)
{
    WebClient wc = new WebClient();
    wc.DownloadDataAsync(new Uri("https://www.dropbox.com/s/l3maq8j3yzciedw/App%20in%205%20minutes.PNG?raw=1"));
    wc.DownloadDataCompleted += Wc_DownloadDataCompleted;
}

private void Wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
    MemoryStream stream = new MemoryStream((byte[])e.Result);
    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(stream);
    bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
    stream.Position = 0;
    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.StreamSource = stream;
    bi.EndInit();
    image1.Source = bi;
}
Neil B
  • 2,096
  • 1
  • 12
  • 23