[ファイルのダウンロード]ダイアログボックスを使用せずに、WebBrowserコントロールを使用してファイルをダウンロードするにはどうすればよいですか?

レオチャピロ

WPFプロジェクトでは、この方法を使用します

webBrowser.Navigate(strUrl);

サーバーからPNG画像を取得します。

次のダイアログが表示されます。

ファイルダウンロードダイアログのスクリーンショット

(ダイアログなしで)画像をシームレスにダウンロードするにはどうすればよいですか?

ニールB

このためにブラウザコントロールを使用する必要はありません。

DownloadFileAsync()を使用してみてください

これが完全に機能するサンプルです。(必要に応じてパスを変更します。)

    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");
    }

ダウンロードしたファイルは、次のようなデフォルトのアプリで開くことができます。

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

編集:

単にpngを表示することが目的の場合は、それをストリームにダウンロードしてから、画像コントロールに表示することができます

完全に機能するサンプル。

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;

これが非同期バージョンです。

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;
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ