2012-05-15 2 views
1

이것은 사용하는 기능으로, 하드 디스크의 웹 사이트에서 가져온 링크에 이미지를 저장하고 싶습니다.이미지를 하드 디스크에 저장하려고하면 오류가 발생합니다. URI 형식은 지원되지 않습니다.

public void GetAllImages() 
{ 

    // Bing Image Result for Cat, First Page 
    string url = "http://www.bing.com/images/search?q=cat&go=&form=QB&qs=n"; 


    // For speed of dev, I use a WebClient 
    WebClient client = new WebClient(); 
    string html = client.DownloadString(url); 

    // Load the Html into the agility pack 
    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 
    doc.LoadHtml(html); 

    // Now, using LINQ to get all Images 
    /*List<HtmlNode> imageNodes = null; 
    imageNodes = (from HtmlNode node in doc.DocumentNode.SelectNodes("//img") 
           where node.Name == "img" 
           && node.Attributes["class"] != null 
           && node.Attributes["class"].Value.StartsWith("sg_t") 
           select node).ToList();*/ 

    var imageLinks = doc.DocumentNode.Descendants("img") 
     .Where(n => n.Attributes["class"].Value == "sg_t") 
     .Select(n => HttpUtility.ParseQueryString(n.Attributes["src"].Value["amp;url"]) 
     .ToList(); 





    foreach (string node in imageLinks) 
    { 
     y++; 
     //Console.WriteLine(node.Attributes["src"].Value); 
     richTextBox1.Text += node + Environment.NewLine; 
     Bitmap bmp = new Bitmap(node); 
     bmp.Save(@"d:\test\" + y.ToString("D6") + ".jpg"); 

     } 

} 

foreach의 맨 아래에 비트 맵을 사용하지만 오류가 발생합니다. 왜 ?

+0

인쇄를 노드 문자열을 비트 맵에 전달을 확인하려면 ctor. 나는 URL (이미지의 src 속성에서)이라고 생각합니다. Bitmap ctor는 파일 이름이 파일을로드 할 것으로 기대하지만 URI에 지원되지 않습니다 (예외로 작성). 웹 요청을 사용하여 원격 URL에서 이미지를 다운로드해야합니다. –

답변

3

먼저 이미지를 다운로드해야합니다. 그런 URI에서 이미지를 저장할 수 없습니다. WebClient를 사용하여 이미지의 바이트를 가져온 다음 해당 데이터를 사용하여 이미지를 만듭니다. 이 같은

뭔가 그런 다음

private System.Drawing.Image GetImage(string URI) 
    { 
     WebClient webClient = new WebClient(); 
     byte[] data = webClient.DownloadData(URI); 
     MemoryStream memoryStream = new MemoryStream(data); 
     return System.Drawing.Image.FromStream(memoryStream); 
    } 

, 나는 그것을 저장 System.Drawing.Image.Save을 사용하는 것이 좋습니다 것입니다 : http://msdn.microsoft.com/en-us/library/system.drawing.image.save.aspx

관련 문제