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);
}
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:
None
0 Points
9 Posts
how to create image from page html passing as string
Sep 01, 2012 08:08 AM|ermunishyadav|LINK
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);
}
Participant
1510 Points
306 Posts
Re: how to create image from page html passing as string
Sep 01, 2012 12:04 PM|ozkary|LINK
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
og-bit.com