开发者

How to save a web page as image

开发者 https://www.devze.com 2023-02-17 04:47 出处:网络
i have a string with webpage source; how can i save i开发者_如何学运维t in byte[] as image?Here: http://pietschsoft.com/post/2008/07/C-Generate-WebPage-Thumbmail-Screenshot-Image.aspx

i have a string with webpage source; how can i save i开发者_如何学运维t in byte[] as image?


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.


Look at Render web page to picture

0

精彩评论

暂无评论...
验证码 换一张
取 消