简体   繁体   中英

How to save a web page as image

i have a string with webpage source; how can i save it 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM