Csharp/C#教程:C#实现在网页中根据url截图并输出到网页的方法分享

本文实例讲述了C#实现在网页中根据url截图并输出到网页的方法。分享给大家供大家参考,具体如下:

网页截图是很多站点的一个小需求,这段代码实现的是如何根据url获得网页截图并输出到网页中。

usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Web; usingSystem.Web.UI; usingSystem.Web.UI.WebControls; usingSystem.Threading; usingSystem.Windows.Forms; usingSystem.Drawing; usingSystem.IO; ///<summary> ///Thispageshowthewayofgenerateaimageinwebsite ///</summary> publicpartialclassDefault2:System.Web.UI.Page { protectedvoidPage_Load(objectsender,EventArgse) { Bitmapm_Bitmap=WebSiteThumbnail.GetWebSiteThumbnail("https://www.google.cn",600,600,600,600); MemoryStreamms=newMemoryStream(); m_Bitmap.Save(ms,System.Drawing.Imaging.ImageFormat.Png);//JPG、GIF、PNG等均可 byte[]buff=ms.ToArray(); Response.BinaryWrite(buff); } } publicclassWebSiteThumbnail { Bitmapm_Bitmap; stringm_Url; intm_BrowserWidth,m_BrowserHeight,m_ThumbnailWidth,m_ThumbnailHeight; publicWebSiteThumbnail(stringUrl,intBrowserWidth,intBrowserHeight,intThumbnailWidth,intThumbnailHeight) { m_Url=Url; m_BrowserHeight=BrowserHeight; m_BrowserWidth=BrowserWidth; m_ThumbnailWidth=ThumbnailWidth; m_ThumbnailHeight=ThumbnailHeight; } publicstaticBitmapGetWebSiteThumbnail(stringUrl,intBrowserWidth,intBrowserHeight,intThumbnailWidth,intThumbnailHeight) { WebSiteThumbnailthumbnailGenerator=newWebSiteThumbnail(Url,BrowserWidth,BrowserHeight,ThumbnailWidth,ThumbnailHeight); returnthumbnailGenerator.GenerateWebSiteThumbnailImage(); } publicBitmapGenerateWebSiteThumbnailImage() { Threadm_thread=newThread(newThreadStart(_GenerateWebSiteThumbnailImage)); m_thread.SetApartmentState(ApartmentState.STA); m_thread.Start(); m_thread.Join(); returnm_Bitmap; } privatevoid_GenerateWebSiteThumbnailImage() { WebBrowserm_WebBrowser=newWebBrowser(); m_WebBrowser.ScrollBarsEnabled=false; m_WebBrowser.Navigate(m_Url); m_WebBrowser.DocumentCompleted+=newWebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted); while(m_WebBrowser.ReadyState!=WebBrowserReadyState.Complete) Application.DoEvents(); m_WebBrowser.Dispose(); } privatevoidWebBrowser_DocumentCompleted(objectsender,WebBrowserDocumentCompletedEventArgse) { WebBrowserm_WebBrowser=(WebBrowser)sender; m_WebBrowser.ClientSize=newSize(this.m_BrowserWidth,this.m_BrowserHeight); m_WebBrowser.ScrollBarsEnabled=false; m_Bitmap=newBitmap(m_WebBrowser.Bounds.Width,m_WebBrowser.Bounds.Height); m_WebBrowser.BringToFront(); m_WebBrowser.DrawToBitmap(m_Bitmap,m_WebBrowser.Bounds); m_Bitmap=(Bitmap)m_Bitmap.GetThumbnailImage(m_ThumbnailWidth,m_ThumbnailHeight,null,IntPtr.Zero); } }

希望本文所述对大家C#程序设计有所帮助。

您可能感兴趣的文章:C#编写ActiveX网页截图控件C#实现属于自己的QQ截图工具C#实现通过ffmpeg从flv视频文件中截图的方法C#实现网页截图功能c#实现winform屏幕截图并保存的示例c#控件截图的简单实例解决C#全屏幕截图的实现方法解决C#截取当前程序窗口指定位置截图的实现方法C#截图程序类似腾讯QQ截图实现代码对指定的网页进行截图的效果C#版C#实现类似qq的屏幕截图程序C#实现的滚动网页截图功能示例

标签: url rl 方法 输出

C++利用opencv实现人脸检测

利用OpenCV实现局部动态阈值分割

上述就是C#学习教程:C#实现在网页中根据url截图并输出到网页的方法分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/906953.html

(0)
上一篇 2021年10月24日
下一篇 2021年10月24日

精彩推荐