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,js,C#),html,webbrowser,url,string,web,object)