WEB(html|html转换为图片(html to jpg)
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Windows.Forms;
//WebBrowser需要 namespace Web { public partial class HtmlToJpg : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string url="";
try { url =Request.QueryString["url"];
} catch { } if (url==null||url == "") return;
Bitmap thumbnail;
thumbnail = GenerateScreenshot(url);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
thumbnail.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
//输出图片 Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;
filename=" +"XXX.jpg");
// Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(ms.GetBuffer());
Response.Flush();
Response.End();
} public Bitmap GenerateScreenshot(string url) { // This method gets a screenshot of the webpage // rendered at its full size (height and width) return GenerateScreenshot(url, -1, -1);
} public Bitmap GenerateScreenshot(string url, int width, int height) { // Load the webpage into a WebBrowser control WebBrowser wb = new WebBrowser();
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
wb.Navigate(url);
while (wb.ReadyState != WebBrowserReadyState.Complete) { System.Windows.Forms.Application.DoEvents();
} // Set the size of the WebBrowser control wb.Width = width;
wb.Height = height;
if (width == -1) { // Take Screenshot of the web pages full width wb.Width = wb.Document.Body.ScrollRectangle.Width;
} if (height == -1) { // Take Screenshot of the web pages full height wb.Height = wb.Document.Body.ScrollRectangle.Height;
} // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
wb.Dispose();
return bitmap;
} } }
【WEB(html|html转换为图片(html to jpg)】 //
推荐阅读
- 一起来学习C语言的字符串转换函数
- django-前后端交互
- 视频转换器哪种好用()
- 私有化轻量级持续集成部署方案--03-部署web服务(下)
- 以太坊中的计量单位及相互转换
- 爬虫数据处理HTML转义字符
- HTML基础--基本概念--跟着李南江学编程
- web网页模板|如此优秀的JS轮播图,写完老师都沉默了
- spring|spring boot项目启动websocket
- OC:|OC: WKWebView详解