Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi all I need to convert the page into the image
but this error is coming

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 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();
}
}


please help me.
Posted
Updated 15-Apr-16 0:09am
v2
Comments
Sinisa Hajnal 15-Apr-16 2:30am    
Why not use the examples? I mean, if you're using asp.net, you have access to winforms. Maybe you could take screenshot of the page? Would that be acceptable?
Sergey Alexandrovich Kryukov 15-Apr-16 2:42am    
You too?
—SA
Sergey Alexandrovich Kryukov 15-Apr-16 2:41am    
System.Windows.Forms?!!! What are you talking about? Who used it? In ASP.NET? You have tremendous fantasy.
—SA
Richard Deeming 15-Apr-16 9:44am    
So let me get this straight: you take a string containing the HTML markup of the page, try to treat that as a Base64-encoded string (which it isn't), and then try to load those bytes as an image (which they're not).

How did you think this code would ever work?!

Quickest and easiest way - use a 3rd party library.

This one[^] will do what you need. I'm not affiliated with them in any way but I've used products from these guys before, always seemed to be good, so happy to recommend them.

[Edit] Actually I think ABCpdf[^] might be better than what I posted above. It'll import straight from HTML and output to various formats (including JPEG and - surprisingly :) - PDF).
 
Share this answer
 
v2
You can do it - sort of - by using the WebBrowser class:
C#
private void GetBitmap(string url)
    {
    WebBrowser loadPage = new WebBrowser();
    loadPage.ScrollBarsEnabled = false;
    loadPage.DocumentCompleted += loadPage_DocumentCompleted;
    loadPage.Navigate(url);
    }
private void loadPage_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
    WebBrowser loadPage = sender as WebBrowser;
    if (loadPage != null)
        {
        loadPage.Width = loadPage.Document.Body.ScrollRectangle.Width;
        loadPage.Height = loadPage.Document.Body.ScrollRectangle.Height;
        using (Bitmap bitmap = new Bitmap(loadPage.Width, loadPage.Height))
            {
            loadPage.DrawToBitmap(bitmap, new Rectangle(0, 0, loadPage.Width, loadPage.Height));
            loadPage.Dispose();
            bitmap.Save(@"D:\Temp\page.jpg", ImageFormat.Jpeg);
            }
        }
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900