2011-01-19 2 views
1

Winforms 앱에서 사이트에 로그인 한 웹 브라우저 컨트롤이 있습니다. 이제 프로그래밍 방식으로 해당 사이트에 로그인 한 경우에만 다운로드 할 수있는 이미지를 다운로드하고 싶습니다.C# winforms webbrowser control - 이미지를 다운로드하는 방법?

그렇다면 내 웹 브라우저 컨트롤에 이미지를 다운로드하라는 말은 어떻게해야합니까? http://www.example.com/image.jpg, 어딘가에 저장 하시겠습니까?

답변

3

파일을 하드 드라이브에 직접 저장하지 않으려면 스트림으로 다운로드 할 수 있습니다. 예 : 당신이 다음 하드 드라이브에 저장하려면

WebClient wc = new WebClient(); 
byte[] bytes = wc.DownloadData("http://www.example.com/image.jpg"); 
Bitmap b = new Bitmap(new MemoryStream(bytes)); 

, 당신은 Bitmap.Save() 메서드를 호출 할 수 있습니다. 예 :

b.Save("bitmap.jpg"); 
0

웹 브라우저를 사용하여 자동으로 수행 할 수없는 것 같습니다. 끝이 IE 인스턴스라는 것을 잊지 마십시오. 당신이 할 수있는 것은 :

WebBrowser wb = new WebBrowseR(); 
wb.Navigate("ImageURL"); 
wb.ShowSaveAsDialog(); 

더 나은 솔루션은 웹 클라이언트

를 사용하여 이미지를 얻을 수 있습니다 : 이미지를 저장하는 사용자에게 대화로 저장을 표시 할 이미지 URL, 다음 호출 ShowSaveAsDialog()로 이동
System.Net.WebClient wc = new System.Net.WebClient(); 
wc.Credentials = new System.Net.NetworkCredential("username", "password"); //Authenticates to the website - Call it only if the image url needs authentication first 
wc.DownloadFile("imageURL", "downloadedImage.jpg"); //Downloads the imageURL to the local file downloadedImage.jpg 
관련 문제