How to save a web page as image

泄露秘密 提交于 2019-12-05 13:32:22

Here: http://pietschsoft.com/post/2008/07/C-Generate-WebPage-Thumbmail-Screenshot-Image.aspx

Is an example of how to use the WebBrowser.DrawToBitmap method.

After you've generated your bitmap, you can compress it using any encoding you want.
This is an example from MSDN for how to compress to PNG (lossless and small):
How to: Encode and Decode a PNG Image

Good luck :)

EDIT:
In order to get the byte array, you might want to use a memory stream as output stream.

Here's a working example of how something like that would work:

public static void Main(string[] args)
{
    byte[] test = new byte[] { 2, 5, 6, 1, 9 };
    MemoryStream ms = new MemoryStream();
    ms.Write(test, 0, 5);

    byte[] image = new byte[ms.Length];
    Buffer.BlockCopy(ms.GetBuffer(), 0, image, 0, (int)ms.Length);
    for (int i = 0; i < ms.Length; i++)
        Console.WriteLine(image[i]);
    Console.ReadKey();
}

And here's an example of how it'd work in your case:

public static void Main(string[] args)
{
    MemoryStream ms = new MemoryStream();
    // You have a PNGBitmapEncoder, and you call this:
    encoder.Save(ms);

    byte[] image = new byte[ms.Length];
    Buffer.BlockCopy(ms.GetBuffer(), 0, image, 0, (int)ms.Length);
    for (int i = 0; i < ms.Length; i++)
        Console.WriteLine(image[i]);
    Console.ReadKey();
}

You may take a look at the following article and this one as well which illustrate one way of taking a screenshot of a webpage and saving it as image. The WPFChromium component also allows you to achieve this without depending on Internet Explorer.

Anton Semenov
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!