2
Answers

How To Convert Page Into Image In ASP.NET

prathap k

prathap k

8y
298
1

What I have tried:

protected void Page_Load(object sender, EventArgs e)
{
//saveURLToImage("http://localhost:2564/Pagetoimage.aspx?OrderId=7");
saveURLToImage("http://www.w3schools.com/");

}

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
byte[] b = Convert.FromBase64String(content);
System.IO.MemoryStream ms = new System.IO.MemoryStream(b);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);

string folderPath = Server.MapPath("~/ImagesFolder/"); //Create a Folder in your Root directory on your solution.
string fileName = "IMageName" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".jpg";
string imagePath = folderPath + fileName;
img.Save(imagePath, System.Drawing.Imaging.ImageFormat.Jpeg);

img.Dispose();
ms.Close();
}
}
but i am getting error like this

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.

please help me.
 
 
 
 
Answers (2)