2014-09-25 3 views
0

로드 된 이미지를 웹 브라우저에서 그림 상자로 복사하거나 복사하는 방법이 있습니까?로드 된 이미지를 웹 브라우저에서 그림 상자로 복사하는 방법

복사하려고하는 이미지는 "captcha"이미지이며 모든 요청이 변경됩니다. 웹 브라우저에서로드 된 이미지가 그림 상자와 동일해야합니다.

img 태그를 분할하여 이미지를 다시 요청하려고했습니다. 그것은 효과가 있지만 사진 상자 이미지는 웹 브라우저가 보여주는 것과 동일하지 않았습니다.

여기까지 제가 한 것입니다. 그것은 하나의 웹 브라우저, 하나의 그림 상자, 하나의 텍스트 상자, 하나 개의 버튼

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Text.RegularExpressions; 
using System.IO; 
using System.Net; 

namespace arman_dobare_kir_mishavad 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      string str2 = webBrowser1.DocumentText; 
      string[] strArray2; 
      strArray2 = Regex.Split(Regex.Split(str2, "<img id=\"content1_imgCaptcha\" src=\"")[1], "\""); 
      textBox1.Text = strArray2[0]; 
      this.pictureBox1.ImageLocation = "http://www.hashkiller.co.uk" + strArray2[0]; 
      return; 
     } 

     public void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
     { 

     } 
    } 
} 
+0

당신이 한 일을 보여주세요. – Shaharyar

+0

괜찮습니다. 내 게시물을 편집했습니다 ... – sci3nt15t

+0

디버깅 할 때'strArray2 [0]'은 무엇입니까? –

답변

6

나에게 해결하기 위해 달을 가져 갔다 내가 꽤 까다로운 일이 포함되어 있습니다. 먼저해야 할 일. 거의 모든 captcha 이미지가 동적으로 생성 된 이미지라는 것을 알았으므로 이미지를 요청할 때마다 url (src 태그)이 같더라도 항상 새로운 captcha 이미지가 생성됩니다.

가장 좋은 방법은 이미로드 된 보안 문자 이미지를 웹 브라우저에서 "스니핑"하는 것입니다. 날 믿어, 이것이 유일한 방법이지. (사용 방법 webbrowser.DrawToBitmap :

좋은 소식은 쉽게하는 방법에 내장

webBrowser.DrawToBitmap (비트 맵, 사각형)

내 샘플 코드와 함께 할 수있다 특정 요소) 그래서

private void button1_Click(object sender, EventArgs e) 
    { 
     int CaptchaWidth = getXoffset(webBrowser1.Document.GetElementById("Captch-Element-Name")); 
     int CaptchaHeight = getYoffset(webBrowser1.Document.GetElementById("Captch-Element-Name")); 

     Bitmap bitmap = new Bitmap(CaptchaWidth, CaptchaHeight); 
     webBrowser1.DrawToBitmap(bitmap, new Rectangle(0, 0, CaptchaWidth, CaptchaHeight)); 

     //now load the image into your pictureBox (you might need to convert the bitmap to a image) 
    } 

    //Methods to get Co-ordinates Of an Element in your webbrowser 
    public int getXoffset(HtmlElement el) 
    { 
     int xPos = el.OffsetRectangle.Left; 
     HtmlElement tempEl = el.OffsetParent; 
     while (tempEl != null) 
     { 
      xPos += tempEl.OffsetRectangle.Left; 
      tempEl = tempEl.OffsetParent; 
     } 
     return xPos; 
    } 

    public int getYoffset(HtmlElement el) 
    { 
     int yPos = el.OffsetRectangle.Top; 
     HtmlElement tempEl = el.OffsetParent; 
     while (tempEl != null) 
     { 
      yPos += tempEl.OffsetRectangle.Top; 
      tempEl = tempEl.OffsetParent; 
     } 
     return yPos; 
    } 

에 대한 나쁜 소식은 C#을)은 MSDN 사이트에서 언급 한 drawtobitmap 방법에 성가신 작은 버그 (가지고있다 . 당신이 그것을 실행할 때 때로는 빈 이미지가 반환됩니다 .... 그래 ... 당신이 Captchas 오른쪽 균열하려고 할 때 정말 원하는 게 아니야!

운 좋게도! 다른 stackOverflow 사용자와 나는 네이티브 GDI +를 사용하는이 메서드의 버그없는 버전에서 작업하는 데 몇 달을 보냈습니다.

완벽하게 작동하므로 drawtobitmap이 예상대로 작동하지 않는 경우 여기를 참조하십시오.

샘플 :

[DllImport("user32.dll")] 
    public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint nFlags); 

    public Bitmap CaptureWindow(Control ctl) 
    { 
     //Bitmap bmp = new Bitmap(ctl.Width, ctl.Height); // includes borders 
     Bitmap bmp = new Bitmap(ctl.ClientRectangle.Width, ctl.ClientRectangle.Height); // content only 
     using (Graphics graphics = Graphics.FromImage(bmp)) 
     { 
      IntPtr hDC = graphics.GetHdc(); 
      try { PrintWindow(ctl.Handle, hDC, (uint)0); } 
      finally { graphics.ReleaseHdc(hDC); } 
     } 
     return bmp; 
    } 

그래서 당신은 단순히 전화 할게 : CaptureWindow (webBrowser1); 전체 웹 브라우저의 이미지를 반환 한 다음 보안 문자 이미지가 포함 된 섹션을 스 니프 아웃합니다.

당신은 내 질문에 (대부분도 응답하지 않은) 내가 여기에 비슷한 문제를 가지고 있었다 볼 수 있습니다

Extracting a image from a WebBrowser Control

Screenshot method generates black images

Reset Webbrowser control to update settings

이제

당신은 보안 문자를 가지고 이미지를 해독해야합니다. 그래서 다른 질문을하고, 내게 링크를 보내고 아픈 것을 내 방법으로 보내라. 당신이 내 악몽을 견뎌야했기 때문에 기쁘다. 이것을 해결책으로 표시하고 유용한 것으로 잊지 마십시오!

관련 문제