如何在ASP.NET中将网页转换为图像 [英] How to convert web page into image in ASP.NET

查看:77
本文介绍了如何在ASP.NET中将网页转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我需要将页面转换为图片

但这个错误即将来临



输入不是有效的基数-64字符串,因为它包含一个非基本的64个字符,两个以上的填充字符,或填充字符中的非法字符。



我有什么试过:



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

//保存到文件

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 /); //在解决方案的根目录中创建一个文件夹。

string fileName =IMageName+ DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now。小时+ DateTime.Now.Minute + DateTime.Now.Second +.jpg;

string imagePath = folderPath + fileName;

img.Save(imagePath,System。 Drawing.Imaging.ImageFormat.Jpeg);



img.Dispose();

ms.Close();

}

}





请帮助我。

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.

推荐答案

最快捷,最简便的方法 - 使用第三方库。



这一个 [ ^ ]将满足您的需求。我不是以任何方式与他们联系,但我之前使用过这些人的产品,似乎总是很好,很乐意推荐他们。



其实我认为 ABCpdf [ ^ ]可能比我上面发布的更好。它将直接从HTML导入并输出为各种格式(包括JPEG和 - 令人惊讶:) - PDF)。
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.

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).


您可以通过使用WebBrowser类来实现 - 排序 - : br />
You can do it - sort of - by using the WebBrowser class:
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);
            }
        }
    }


这篇关于如何在ASP.NET中将网页转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆