在项目中做了个网页略缩图生成的功能,在网上能找到一些方法,但是没有找到在C#的MVC框架中写的具体方法,然后我根据搜到一些参考代码写了一个用于C#MVC的方法。非常感谢博客园 Sampson-Li 大神提供的参考代码(http://www.cnblogs.com/liguanghui/archive/2012/02/13/2349244.html),以及第七城市网http://www.th7.cn/Program/net/201310/154794.shtml 提供的参考代码。
本方法只需要前台传给一个参数,即网页地址,然后成功返回的网页略缩图保存的相对路径。
1 #region 根据网址生成页面略缩图
2 /// <summary>
3 /// 根据网址生成页面略缩图 by韦明友 20140805
4 /// </summary>
5 /// <param name="webUrl">网址</param>
6 /// <returns>返回json数据</returns>
7 [HttpPost]
8 [STAThread]
9 public JsonResult CreateImgByWebUrl(String webUrl="")
10 {
11 var result = new BaseJsonData() { flag = false };
12 if (webUrl != "")
13 {
14 //委托WebThumbnails方法,合并线程
15 Thread thread = new Thread(new ThreadStart(delegate() { result.msg = WebThumbnails(webUrl); }));
16 thread.SetApartmentState(ApartmentState.STA);
17 thread.IsBackground = true;
18 thread.Start();
19 }
20 //挂起当前线程
21 Thread.Sleep(2000);
22 return Json(result, JsonRequestBehavior.AllowGet);
23 }
24 #endregion 生成网页略缩图
25
26 #region 生成网页略缩图
27 /// <summary>
28 /// 生成网页略缩图 by韦明友 20140806
29 /// </summary>
30 /// <param name="_weburl">网址</param>
31 /// <returns></returns>
32 public string WebThumbnails(string _weburl) {
33 string msg = null;
34 /** 浏览器的宽、高 **/
35 int bWidth = 0, bHeight = 0;
36 //获取浏览器
37 WebBrowser webBrowser = new WebBrowser();
38 webBrowser.ScrollBarsEnabled = true;
39 webBrowser.ScriptErrorsSuppressed = true;
40 webBrowser.AllowNavigation = true;
41 webBrowser.Navigate(_weburl);
42 //获取浏览器的DOM元素
43 while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
44 {
45 Application.DoEvents();
46 }
47 if (webBrowser.Document != null)
48 {
49 HtmlDocument htmlDom = webBrowser.Document;
50 bWidth = int.Parse(htmlDom.Body.GetAttribute("scrollWidth"));
51 bHeight = int.Parse(htmlDom.Body.GetAttribute("scrollHeight"));
52 webBrowser.Width = bWidth;
53 webBrowser.Height = bHeight;
54 }
55 //位图
56 Bitmap bitmap = new Bitmap(bWidth, bHeight);
57 bitmap.MakeTransparent();
58 Rectangle rectangle = new Rectangle(0, 0, bWidth, bHeight);
59 webBrowser.DrawToBitmap(bitmap, rectangle);
60 Image imgOutput = bitmap;
61 Image oThumbNail = new Bitmap(bWidth, bHeight, imgOutput.PixelFormat);
62 //绘图
63 Graphics g = Graphics.FromImage(oThumbNail);
64 g.CompositingQuality = CompositingQuality.HighQuality;
65 g.SmoothingMode = SmoothingMode.HighQuality;
66 g.InterpolationMode = InterpolationMode.High;
67 g.Clear(Color.Transparent);
68 Rectangle orectangle = new Rectangle(0, 0, bWidth, bHeight);
69 g.DrawImage(imgOutput, orectangle);
70 //Bitmap resultBitmap = null;
71 try
72 {
73 //resultBitmap = (Bitmap)oThumbNail;
74 string savePath = System.Configuration.ConfigurationManager.AppSettings["ImgFile"].ToString();
75 String imageFilePath = "WebImage/";
76 string imageName = "web_url" + DateTime.Now.ToFileTime() + ".jpg";
77 savePath = Server.MapPath(savePath + imageFilePath);
78
79 if (!Directory.Exists(savePath)) {
80 Directory.CreateDirectory(savePath);
81 }
82 //savePath = Server.MapPath(savePath + imageName);
83 savePath = Path.Combine(savePath,imageName);
84 bitmap.Dispose();
85 //resultBitmap.Save(savePath, ImageFormat.Jpeg);
86 oThumbNail.Save(savePath,ImageFormat.Jpeg);
87 msg = ConfigurationManager.AppSettings["ImgFile"].ToString() + imageFilePath + imageName;
88 }
89 catch (Exception ex)
90 {
91 Console.WriteLine(ex.Message);
92 return null;
93 }
94 finally
95 {
96 imgOutput.Dispose();
97 imgOutput = null;
98 webBrowser.Dispose();
99 webBrowser = null;
100 }
101 return msg;
102 }
103 #endregion
View Code
本功能的实现主要分为两个方法来实现。CreateImgByWebUrl 方法为主要方法,在该方法中,创建了一个线程,委托了 WebThumbnails 方法。WebThumbnails 方法是核心生成网页略缩图方法。
关于委托的理解,我很感谢博客园 icerain 大神提供的参考(http://www.cnblogs.com/doubaokun/archive/2008/11/27/1341915.html),以及博客园 Jimmy Zhang 大神提供的参考(http://www.cnblogs.com/JimmyZhang/archive/2007/09/23/903360.html)。
转载于:https://www.cnblogs.com/youler/p/3897278.html