2010-06-10 130 views
4

我想將* .aspx(HTML)頁面(用戶界面)轉換爲圖像,如JPEG。 我使用下面的代碼爲如何將網頁轉換爲圖像?

Protected Sub btnGet_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGet.Click 
     saveURLToImage("http://google.co.in") 
End Sub 

Private Sub saveURLToImage(ByVal url As String) 
     If Not String.IsNullOrEmpty(url) Then 
      Dim content As String = "" 

      Dim webRequest__1 As System.Net.WebRequest = WebRequest.Create(url) 
      Dim webResponse As System.Net.WebResponse = webRequest__1.GetResponse() 
      Dim sr As System.IO.StreamReader = New StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8")) 
      content = sr.ReadToEnd() 
      'save to file 
      Dim b As Byte() = Convert.FromBase64String(content) 
      Dim ms As New System.IO.MemoryStream(b, 0, b.Length) 
      Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(ms) 
      img.Save("c:\pic.jpg", System.Drawing.Imaging.ImageFormat.Jpeg) 

      img.Dispose() 
      ms.Close() 
     End If 
    End Sub 

但是我收到錯誤爲「在鹼-64串無效字符」在線 昏暗b以字節()= Convert.FromBase64String(內容)

+0

是否要保存HTML代碼爲JPG或者呈現的html(就像你在瀏覽器中打開url時看到的那樣)? – 2010-06-10 08:35:27

+0

我只是問,因爲你的代碼建議第一個。即使你解決了你的錯誤,它也不會工作。你不能只是拿一個字符串,並保存爲一個像這樣的jpeg。你必須以某種形式使用DrawString() – 2010-06-10 08:42:45

回答

6

您沒有使用webRequest獲取頁面的渲染圖像,只是HTML代碼的gettig。

要生成一個圖像追隨該代碼(直接從this post撕開)

public Bitmap GenerateScreenshot(string url) 
{ 
    // This method gets a screenshot of the webpage 
    // rendered at its full size (height and width) 
    return GenerateScreenshot(url, -1, -1); 
} 

public Bitmap GenerateScreenshot(string url, int width, int height) 
{ 
    // Load the webpage into a WebBrowser control 
    WebBrowser wb = new WebBrowser(); 
    wb.ScrollBarsEnabled = false; 
    wb.ScriptErrorsSuppressed = true; 
    wb.Navigate(url); 
    while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); } 


    // Set the size of the WebBrowser control 
    wb.Width = width; 
    wb.Height = height; 

    if (width == -1) 
    { 
     // Take Screenshot of the web pages full width 
     wb.Width = wb.Document.Body.ScrollRectangle.Width; 
    } 

    if (height == -1) 
    { 
     // Take Screenshot of the web pages full height 
     wb.Height = wb.Document.Body.ScrollRectangle.Height; 
    } 

    // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control 
    Bitmap bitmap = new Bitmap(wb.Width, wb.Height); 
    wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height)); 
    wb.Dispose(); 

    return bitmap; 
} 

以下是上述方法的一些示例用法:

// Generate screenshot of a webpage at 1024x768 resolution 
Bitmap screenshot = GenerateScreenshot("http://pietschsoft.com", 1024, 768); 

// Generate screenshot of a webpage at the webpage's full size (height and width) 
screenshot = GenerateScreenshot("http://pietschsoft.com"); 

// Display screenshot in PictureBox control 
pictureBox1.Image = thumbnail; 

/* 
// Save screenshot to a File 
screenshot.Save("screenshot.png", System.Drawing.Imaging.ImageFormat.Png); 
*/