locked
how to create image from page html passing as string RRS feed

  • Question

  • User-903901682 posted

     
    hi

        I am using this code to coonvert page html into image
        but i am getting error in line
        (System.Drawing.Image img = System.Drawing.Image.FromStream(ms);)
        
       Error is : (Parameter is not valid.)


    full code here:

    string content = "";
            System.Net.WebRequest webRequest = WebRequest.Create("http://localhost:58534/EFTestApp/ScreenShot.aspx");
            System.Net.WebResponse webResponse = webRequest.GetResponse();
            System.IO.StreamReader sr = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
            content = sr.ReadToEnd().Trim();
            //save to file
            byte[] bytesToEncode = Encoding.UTF8.GetBytes(content);
            string encodedText = Convert.ToBase64String(bytesToEncode);

            byte[] b = Convert.FromBase64String(encodedText);
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(b, 0, b.Length))
            {
                System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
                img.Save(Server.MapPath("screenshot/myscreenshot.bmp"), System.Drawing.Imaging.ImageFormat.Jpeg);
            }

    Saturday, September 1, 2012 8:08 AM

Answers

  • User108830567 posted

    The System.Drawing.Image.FromStream class expects a stream with bytes from an image. You are passing html instead which is not the expected format. To verify this, replace your url to download a specific image like:

     System.Net.WebRequest webRequest = WebRequest.Create("http://localhost:58534/EFTestApp/images/animage.jpg");

    Your current code should work. 

      To capture a screen shot of a webpage, you could use the WebBrowser class which has this method DrawToBitmap.

    hope it helps

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 1, 2012 12:04 PM