0

i want to convert a Html page into image file. for this i used code

My Code :

protected void Button1_Click(object sender, EventArgs e)
{

    saveURLToImage("http://cg.nic.in/mahasamund/home.htm");

}
private void saveURLToImage(string url)
{
    if (!string.IsNullOrEmpty(url))
    {
        string content = "";

        System.Net.WebRequest webRequest = WebRequest.Create(url);
        System.Net.WebResponse webResponse = webRequest.GetResponse();
        System.IO.StreamReader sr = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
        content = sr.ReadToEnd();
        //save to file
        System.Windows.Forms.MessageBox.Show(content);
        byte[] b = Convert.FromBase64String(content);
        System.IO.MemoryStream ms = new System.IO.MemoryStream(b);
        System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
        img.Save(@"d:\pic.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

        img.Dispose();
        ms.Close();
    }
}

when i am run this code and click on Button then error occurred i.e.

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

What is the problem? How shall I do this?

ekad
  • 14,436
  • 26
  • 44
  • 46
chitrakant sahu
  • 211
  • 3
  • 8
  • 18
  • I already have this need. In that time I used `PhantomJS`, an `*.exe` that open the page like a browser and you can take screen shots of this. – Vitor Canova Feb 05 '14 at 11:46

2 Answers2

0

You probably have "\0" characters in your string and that is not a valid base64 character.

I would try to use:

content.Trim("\0");

before you are converting to base64

ohlmar
  • 958
  • 8
  • 17
0
byte[] b = Convert.FromBase64String(content);

It seems your content contains sth illegal. (Maybe some tag) Try to encode it before passing to the FromBase64String method.