本文介绍了如何将网页转换为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换*的.aspx(HTML)页面的(用户界面)到图像,如JPEG。
我使用低于code为

I want to convert *.aspx (HTML) page's (User Interface) to Image such as JPEG.I am using below code for that

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

但我得到的错误是在一个base-64字符串无效字符在行
昏暗b以字节()= Convert.FromBase64String(内容)

But I am getting Error as "Invalid character in a Base-64 string" at lineDim b As Byte() = Convert.FromBase64String(content)

推荐答案

您没有得到使用的WebRequest页面的渲染图像,你gettig只有HTML code。

You are not getting a rendered image of the page using webRequest, you are gettig only the HTML code.

要生成一个图像遵循这一code(直接的)

To generate a image follow this code (ripped directly from 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;
}

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

Here are some example usages of the above method:

// 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);
*/

这篇关于如何将网页转换为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:55
查看更多