代码下载地址:
http://download.csdn.net/source/1669301
SaveHtMLtoImageComm 共通模块类定义
GetImage.cs 类 定义
using System; using System.Collections.Generic; using System.Text; using System.Drawing; namespace SaveHtMLtoImageComm { public class GetImage { private int S_Height; private int S_Width; private int F_Height; private int F_Width; private string MyURL; public int ScreenHeight { get { return S_Height; } set { S_Height = value; } } public int ScreenWidth { get { return S_Width; } set { S_Width = value; } } public int ImageHeight { get { return F_Height; } set { F_Height = value; } } public int ImageWidth { get { return F_Width; } set { F_Width = value; } } public string WebSite { get { return MyURL; } set { MyURL = value; } } public GetImage(string WebSite, int ScreenWidth, int ScreenHeight, int ImageWidth, int ImageHeight) { this.WebSite = WebSite; this.ScreenWidth = ScreenWidth; this.ScreenHeight = ScreenHeight; this.ImageHeight = ImageHeight; this.ImageWidth = ImageWidth; } public Bitmap GetBitmap() { WebPageBitmap Shot = new WebPageBitmap(this.WebSite, this.ScreenWidth, this.ScreenHeight); Shot.GetIt(); Bitmap Pic = Shot.DrawBitmap(this.ImageHeight, this.ImageWidth); return Pic; } } }
WebPageBitmap.cs类 定义
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; using System.Net; using System.Runtime.InteropServices; namespace SaveHtMLtoImageComm { public struct Struct_INTERNET_PROXY_INFO { public int dwAccessType; public IntPtr proxy; public IntPtr proxyBypass; }; public class WebPageBitmap { [DllImport("wininet.dll", SetLastError = true)] private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength); private void RefreshIESettings(string strProxy) { const int INTERNET_OPTION_PROXY = 38; const int INTERNET_OPEN_TYPE_PROXY = 3; Struct_INTERNET_PROXY_INFO struct_IPI; // Filling in structure struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY; struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy); struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local"); // Allocating memory IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI)); // Converting structure to IntPtr Marshal.StructureToPtr(struct_IPI, intptrStruct, true); bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI)); } WebBrowser MyBrowser; string URL; int Height; int Width; public WebPageBitmap(string url, int width, int height) { RefreshIESettings("www.my400800.cn:80"); this.Height = height; this.Width = width; this.URL = url; MyBrowser = new WebBrowser(); WebProxy proxy = new WebProxy("blog.my400800.cn", 8080); //定義一個網關對象 // MyBrowser. .Proxy = proxy; MyBrowser.ScrollBarsEnabled = false; MyBrowser.Size = new Size(this.Width, this.Height); } public void GetIt() { MyBrowser.Navigate(this.URL); while (MyBrowser.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); } } public Bitmap DrawBitmap(int theight, int twidth) { Bitmap myBitmap = new Bitmap(Width, Height); Rectangle DrawRect = new Rectangle(0, 0, Width, Height); MyBrowser.DrawToBitmap(myBitmap, DrawRect); System.Drawing.Image imgOutput = myBitmap; System.Drawing.Image oThumbNail = new Bitmap(twidth, theight, imgOutput.PixelFormat); Graphics g = Graphics.FromImage(oThumbNail); g.CompositingQuality = CompositingQuality.HighSpeed; g.SmoothingMode = SmoothingMode.HighSpeed; g.InterpolationMode = InterpolationMode.HighQualityBilinear; Rectangle oRectangle = new Rectangle(0, 0, twidth, theight); g.DrawImage(imgOutput, oRectangle); try { return (Bitmap)oThumbNail; } catch (Exception ex) { return null; } finally { imgOutput.Dispose(); imgOutput = null; MyBrowser.Dispose(); MyBrowser = null; } } } }
main.cs类 定义
using System; using System.Collections.Generic; using System.Text; using System.Web; using System.Threading; using System.Web.UI; namespace SaveHtMLtoImageComm { public class main { public string strURl = ""; public string strImgFileName = ""; public Page Context = null; public void startImg() { string url = strURl; //GetImage thumb = new GetImage(url, 1024, 4000, 1024, 4000); GetImage thumb = new GetImage(url, 1024, 1024, 1024, 1024); System.Drawing.Bitmap x = thumb.GetBitmap(); //string FileName = DateTime.Now.ToString("yyyyMMddhhmmss"); x.Save(Context.Server.MapPath("~") + @"/image/" + strImgFileName);// FileName + ".jpg"); } } }
html转换图片调用测试方法
Default.aspx类 定义
using System; using System.Collections.Generic; using System.Text; using System.Web; using System.Threading; using System.Web.UI; namespace SaveHtMLtoImageComm { public class main { public string strURl = ""; public string strImgFileName = ""; public Page Context = null; public void startImg() { string url = strURl; //GetImage thumb = new GetImage(url, 1024, 4000, 1024, 4000); GetImage thumb = new GetImage(url, 1024, 1024, 1024, 1024); System.Drawing.Bitmap x = thumb.GetBitmap(); //string FileName = DateTime.Now.ToString("yyyyMMddhhmmss"); x.Save(Context.Server.MapPath("~") + @"/image/" + strImgFileName);// FileName + ".jpg"); } } }
Default.aspx.cs类 定义
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using SaveHtMLtoImageComm; using System.Threading; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { //string url = "http://www.my400800.cn"; //GetImage thumb = new GetImage(url, 1024, 4000, 1024, 4000); //System.Drawing.Bitmap x = thumb.GetBitmap(); //string FileName = DateTime.Now.ToString("yyyyMMddhhmmss"); //x.Save(Server.MapPath("~")+ @"/" + FileName + ".jpg"); //Console.WriteLine("成功"); //Image1.ImageUrl = ""; thread(); } private void thread() { main m = new main(); m.strURl =txt_url.Text; m.strImgFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg"; m.Context = Page; Thread th = new Thread(new ThreadStart(m.startImg)); th.SetApartmentState(ApartmentState.STA); th.Start(); while (th.IsAlive) { Thread.Sleep(100); } Image1.ImageUrl = "image/" + m.strImgFileName; Image1.Visible = true; } }