C# 自动执行WebBrowser.ShowSaveAsDialog()或其他方法

C# 自动执行WebBrowser.ShowSaveAsDialog()或其他方法,c#,webbrowser-control,download,webclient-download,imagedownload,C#,Webbrowser Control,Download,Webclient Download,Imagedownload,基本上,我不想保存在webBrowser控件中加载的图像。目前我唯一能让它工作的方法是显示“另存为”对话框 有没有办法通过一条小路,让它自救?(隐藏我要求显示的对话框!) 还有其他保存图像的方法吗?我似乎无法让documentstream工作。我还尝试了webclient.filedownload(…),但得到了一个错误302(((302)find Redirect.)) DownloadFileAsync只给出一个空jpeg文件,没有错误 文件总是JPEG格式,但不总是在同一个位置。您应该使用

基本上,我不想保存在webBrowser控件中加载的图像。目前我唯一能让它工作的方法是显示“另存为”对话框

有没有办法通过一条小路,让它自救?(隐藏我要求显示的对话框!)

还有其他保存图像的方法吗?我似乎无法让documentstream工作。我还尝试了
webclient.filedownload(…)
,但得到了一个错误302(((302)find Redirect.))

DownloadFileAsync
只给出一个空jpeg文件,没有错误

文件总是JPEG格式,但不总是在同一个位置。

您应该使用

它具有自动跟踪302的能力

var myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.contoso.com"); 
//increase this number if there are more then one redirects. 
myHttpWebRequest.MaximumAutomaticRedirections = 1;
myHttpWebRequest.AllowAutoRedirect = true;
var myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();    

var buff = new byte[myHttpWebResponse.ContentLength];

// here you specify the path to the file. The path in this example is : image.jpg
// if you want to store it in the application root use:
// AppDomain.CurrentDomain.BaseDirectory + "\\image.jpg"
using (var sw = new BinaryWriter(File.Open("c:\\image.jpg", FileMode.OpenOrCreate)))
{
    using (var br = new BinaryReader (myHttpWebResponse.GetResponseStream ()))
    {
        br.Read(buff, 0, (int)myHttpWebResponse.ContentLength);
        sw.Write(buff);
    }            
}

是的,我在别处读到过,那应该行得通,我试过你的代码,但还是得到了302?文件保存在哪里?(如何指定输出路径)文件中有一个小错误。:)试试这个。你能共享指向重定向图像的链接吗?有人告诉我不能共享数据。仍然在第4行中出现重定向错误。我可以阻止重定向吗?我不明白为什么会发生这种情况,当您在web浏览器中查看配置文件图片时,它显示良好并停留在图像上,不会重定向到任何地方?如果我设置:myHttpWebRequest.AllowAutoRedirect=false;它会转到下一行,但是对于字节来说太大了?不,它会在第4行出现错误并停止,如果您将autoredirect更改为false,我会转到下一行,希望这是一个进步。