4

i have a string with webpage source; how can i save it in byte[] as image?

r.r
  • 7,023
  • 28
  • 87
  • 129
  • Do you mean `byte[]`? And is the url to an image, or is it of a website that you need to render? – Aidiakapi Mar 17 '11 at 10:36
  • yes byte, i have a hole HTML of webpage. saved in sprint as Pagesource. i need to make an image from it and save as byte[] in ms sql database. – r.r Mar 17 '11 at 10:38
  • http://stackoverflow.com/questions/2434156/webbrowser-drawtobitmap-or-other-methods – Felice Pollano Mar 17 '11 at 10:40

3 Answers3

4

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();
}
Aidiakapi
  • 6,034
  • 4
  • 33
  • 62
2

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

Look at Render web page to picture

Community
  • 1
  • 1
Anton Semenov
  • 6,227
  • 5
  • 41
  • 69