0

I am trying to convert the local HTML file to bitmap image using C# Windows Form Application. For that I am reading the HTML file by using memory stream. But after passing the memory stream to Bitmap object it saying "Parameter is not valid".

Below is the sample code

MemoryStream stm = new MemoryStream(data); Bitmap f_Bitmap = (Bitmap)Image.FromStream(stm);

Please provide the solution for how can i covert the HTML file to bitmap image.

Thanks.

Mayuresh
  • 423
  • 1
  • 6
  • 24
  • http://stackoverflow.com/questions/7803201/capturing-webpage-as-image-in-c-ensuring-javascript-rendered-elements-are-visi – L.B Jul 09 '12 at 13:18
  • When you say HTML file, do you mean an html file that has a link in it? or is the HTML file the direct link? e.g. http://www.example.com/mypicture.png What are the contents of the data? – Tom Jul 09 '12 at 13:19

4 Answers4

1

You can always use the WebBrowser object (read more here).

    public void WBCapture()
    {
        WebBrowser wb = new WebBrowser();
        wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);

        wb.ScrollBarsEnabled = true;
        wb.Width = 800;
        wb.Height = 600;
        wb.DocumentText = @"<b>Hello</b> <i>World</i>!!!";
        // Or you can navigate to:
        // wb.Navigate("http://mydocmentsurl.com");
    }

    void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser wb = (WebBrowser)sender;

        using (Graphics graphics = wb.CreateGraphics())
        using (Bitmap bitmap = new Bitmap(wb.Width, wb.Height, graphics))
        {
            Rectangle bounds = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            wb.DrawToBitmap(bitmap, bounds);
            bitmap.Save(@"C:\caputre.png", ImageFormat.Png);
        }
    }
frontlinebg
  • 423
  • 5
  • 5
0

My explanation is on the error your receiving :

The reason it is throwing error cause all you have done is pushed HTML text from the file in to the stream and when FromStream uses the data it says the format of the data is not a bitmap or good enough for it

HatSoft
  • 11,077
  • 3
  • 28
  • 43
0

If you want to save the contents of an HTML file as a Bitmap you cannot just put the bytes into a Bitmap object. You'll need to use a WebBrowser object and draw it manually. Here is an article that outlines it.

Fr33dan
  • 4,227
  • 3
  • 35
  • 62
-1

This will work as long as the HTML file you speak of is a direct link to the image file:

            var request = (HttpWebRequest) WebRequest.Create(IMAGE_URL);

            using (var stream = request.GetResponse().GetResponseStream())
            {
                using (var image = Image.FromStream(stream))
                {
                    var bitmap = new Bitmap(image);
                    //use or return bitmap, image will automatically get disposed
                }
            }

Replace IMAGE_URL with your URL.

If the HTML file contains the URL as text, then you will need to parse it out.

If this file is local, you can simply use a FileStream (or FileReader) to get a stream of the local file (this is similar to GetResponseStream on a WebRequest.)

Let me know if you have questions.

Hope it helps.

Tom
  • 2,360
  • 21
  • 15