国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET實現(xiàn)根據(jù)URL生成網(wǎng)頁縮略圖的方法

2024-07-10 13:29:39
字體:
供稿:網(wǎng)友

這篇文章主要介紹了ASP.NET實現(xiàn)根據(jù)URL生成網(wǎng)頁縮略圖的方法,結(jié)合實例較為詳細的分析了asp.net生成網(wǎng)頁縮略圖的詳細實現(xiàn)技巧與相關注意事項,需要的朋友可以參考下

本文實例講述了ASP.NET實現(xiàn)根據(jù)URL生成網(wǎng)頁縮略圖的方法。分享給大家供大家參考,具體如下:

工作中需要用到根據(jù)URL生成網(wǎng)頁縮略圖功能,提前做好準備。

在網(wǎng)上找了份源碼,但是有錯誤:當前線程不在單線程單元中,因此無法實例化 ActiveX 控件“8856f961-340a-11d0-a9”,解決后運行良好,記錄在此備用!

起始頁:Default.aspx

 

 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CaptureToImage._Default" %> 
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  4. <html xmlns="http://www.w3.org/1999/xhtml" > 
  5. <head id="Head1" runat="server"
  6. <title>Snap</title> 
  7. </head> 
  8. <body> 
  9. <form id="form1" runat="server"
  10. <div> 
  11. <input type="button" onclick="window.open('Snap.aspx?url=www.njude.com.cn')" value="生成網(wǎng)頁縮略圖"/> 
  12. </div> 
  13. </form> 
  14. </body> 
  15. </html> 

調(diào)用頁:Snap.aspx

 

 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Snap.aspx.cs" Inherits="CaptureToImage.Snap" AspCompat="true" %> 
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  3. <html xmlns="http://www.w3.org/1999/xhtml" > 
  4. <head runat="server"
  5. <title>無標題頁</title> 
  6. </head> 
  7. <body> 
  8. <form id="form1" runat="server"
  9. <div> 
  10. </div> 
  11. </form> 
  12. </body> 
  13. </html> 

PS:紅色字體部分是為解決錯誤增加的代碼,強制程序在單線程環(huán)境下運行!

調(diào)用頁:Snap.aspx.cs

 

 
  1. using System; 
  2. using System.Data; 
  3. using System.Configuration; 
  4. using System.Collections; 
  5. using System.Web; 
  6. using System.Web.Security; 
  7. using System.Web.UI; 
  8. using System.Web.UI.WebControls; 
  9. using System.Web.UI.WebControls.WebParts; 
  10. using System.Web.UI.HtmlControls; 
  11. using System.Drawing.Imaging; 
  12. namespace CaptureToImage 
  13. public partial class Snap : System.Web.UI.Page 
  14. protected void Page_Load(object sender, EventArgs e) 
  15. string url = string.Empty; 
  16. url = Request.QueryString[0]; 
  17. try 
  18. GetImage thumb = new GetImage(url, 1024, 768, 800, 600); 
  19. System.Drawing.Bitmap x = thumb.GetBitmap(); 
  20. x.Save(Response.OutputStream, ImageFormat.Jpeg); 
  21. Response.ContentType = "image/jpeg"
  22. catch (Exception ex) 
  23. Response.Write(ex.Message); 

類文件:GetImage.cs

 

 
  1. using System; 
  2. using System.Drawing; 
  3. using System.Drawing.Imaging; 
  4. using System.Windows.Forms; 
  5. using System.Web.UI; 
  6. namespace CaptureToImage 
  7. public class GetImage 
  8. int S_Height; 
  9. int S_Width; 
  10. int F_Height; 
  11. int F_Width; 
  12. string MyURL; 
  13. public int ScreenHeight 
  14. get 
  15. return S_Height; 
  16. set 
  17. S_Height = value; 
  18. public int ScreenWidth 
  19. get 
  20. return S_Width; 
  21. set 
  22. S_Width = value; 
  23. public int ImageHeight 
  24. get 
  25. return F_Height; 
  26. set 
  27. F_Height = value; 
  28. public int ImageWidth 
  29. get 
  30. return F_Width; 
  31. set 
  32. F_Width = value; 
  33. public string WebSite 
  34. get 
  35. return MyURL; 
  36. set 
  37. MyURL = value; 
  38. public GetImage(string WebSite, int ScreenWidth, int ScreenHeight, int ImageWidth, int ImageHeight) 
  39. this.WebSite = WebSite; 
  40. this.ScreenHeight = ScreenHeight; 
  41. this.ScreenWidth = ScreenWidth; 
  42. this.ImageHeight = ImageHeight; 
  43. this.ImageWidth = ImageWidth; 
  44. [STAThread] 
  45. public Bitmap GetBitmap() 
  46. WebPageBitmap Shot = new WebPageBitmap(this.WebSite, this.ScreenWidth, this.ScreenHeight); 
  47. Shot.GetIt(); 
  48. Bitmap Pic = Shot.DrawBitmap(this.ImageHeight, this.ImageWidth); 
  49. return Pic; 
  50. public class WebPageBitmap 
  51. WebBrowser MyBrowser; 
  52. string URL; 
  53. int Height; 
  54. int Width; 
  55. public WebPageBitmap(string url, int width, int height) 
  56. this.URL = url; 
  57. this.Width = width; 
  58. this.Height = height; 
  59. MyBrowser = new WebBrowser(); 
  60. MyBrowser.ScrollBarsEnabled = false
  61. MyBrowser.Size = new Size(this.Width, this.Height); 
  62. public void GetIt() 
  63. NavigateUrl(this.URL); 
  64. while (MyBrowser.ReadyState != WebBrowserReadyState.Complete) 
  65. Application.DoEvents(); 
  66. public delegate void DelUserHandler(string url); 
  67. public void NavigateUrl(string url) 
  68. try 
  69. if (this.MyBrowser.InvokeRequired) 
  70. DelUserHandler handler = new DelUserHandler(NavigateUrl); 
  71. MyBrowser.Invoke(handler, url); 
  72. else 
  73. this.MyBrowser.Navigate(url); 
  74. catch (Exception ex) 
  75. throw new Exception("NavigateUrl()" + ex.Message); 
  76. public Bitmap DrawBitmap(int theight, int twidth) 
  77. Bitmap myBitmap = new Bitmap(this.Width, this.Height); 
  78. Rectangle DrawRect = new Rectangle(0, 0, this.Width, this.Height); 
  79. MyBrowser.DrawToBitmap(myBitmap, DrawRect); 
  80. System.Drawing.Image imgOutput = myBitmap; 
  81. System.Drawing.Bitmap oThumbNail = new Bitmap(twidth, theight, imgOutput.PixelFormat); 
  82. Graphics g = Graphics.FromImage(oThumbNail); 
  83. g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed; 
  84. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed; 
  85. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; 
  86. Rectangle oRectangle = new Rectangle(0, 0, twidth, theight); 
  87. g.DrawImage(imgOutput, oRectangle); 
  88. try 
  89. return oThumbNail; 
  90. catch 
  91. return null
  92. finally 
  93. imgOutput.Dispose(); 
  94. imgOutput = null
  95. MyBrowser.Dispose(); 
  96. MyBrowser = null

PS:項目中需要添加引用System.Windows.Forms

希望本文所述對大家asp.net程序設計有所幫助。


注:相關教程知識閱讀請移步到ASP.NET教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 隆安县| 河源市| 和硕县| 云霄县| 广州市| 左权县| 文化| 井陉县| 瓮安县| 泽库县| 游戏| 淮阳县| 玛沁县| 田东县| 澄江县| 宽甸| 洛南县| 山东省| 静安区| 平顺县| 宜宾市| 延吉市| 深州市| 蕉岭县| 阿瓦提县| 惠州市| 兴文县| 沅陵县| 清镇市| 贡嘎县| 永宁县| 合作市| 类乌齐县| 牟定县| 聊城市| 盘锦市| 南召县| 那坡县| 肥城市| 肥城市| 龙山县|