How do I get a bitmap of the WatiN image element?

前端 未结 5 2116
暗喜
暗喜 2020-12-17 01:27

I have some textfields processed and other elements, but I want to get the bitmap so I can save it somewhere on disk. I need to do it directly from WatiN if this is possible

5条回答
  •  清歌不尽
    2020-12-17 02:10

    I don't think you can get the binary information directly from WatiN. However you have Image.Uri method give you the URI of the image. So then it is easy to download it wih http request.

    using (Browser browser = new IE("http://www.sp4ce.net/computer/2011/01/06/how-to-use-WatiN-with-NUnit.en.html"))
    {
       Image image = browser.Images[0];
       Console.Write(image.Uri);
    
       HttpWebRequest request = (HttpWebRequest)WebRequest.Create(image.Uri);
       WebResponse response = request.GetResponse();
       using (Stream stream = response.GetResponseStream())
       using (FileStream fs = File.OpenWrite(@"c:\foo.png"))
       {
          byte[] bytes = new byte[1024];
          int count;
          while((count = stream.Read(bytes, 0, bytes.Length))!=0)
          {
             fs.Write(bytes, 0, count);
          }
       }
    }
    

    Hope this helps

提交回复
热议问题