C#实现在网页中根据url截图并输出到网页的方法
程序员文章站
2022-04-10 09:55:58
本文实例讲述了c#实现在网页中根据url截图并输出到网页的方法。分享给大家供大家参考,具体如下:
网页截图是很多站点的一个小需求,这段代码实现的是如何根据url获得网页截...
本文实例讲述了c#实现在网页中根据url截图并输出到网页的方法。分享给大家供大家参考,具体如下:
网页截图是很多站点的一个小需求,这段代码实现的是如何根据url获得网页截图并输出到网页中。
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.threading; using system.windows.forms; using system.drawing; using system.io; /// <summary> /// this page show the way of generate a image in website /// </summary> public partial class default2 : system.web.ui.page { protected void page_load(object sender, eventargs e) { bitmap m_bitmap = websitethumbnail.getwebsitethumbnail("http://www.google.cn", 600, 600, 600, 600); memorystream ms = new memorystream(); m_bitmap.save(ms, system.drawing.imaging.imageformat.png);//jpg、gif、png等均可 byte[] buff = ms.toarray(); response.binarywrite(buff); } } public class websitethumbnail { bitmap m_bitmap; string m_url; int m_browserwidth, m_browserheight, m_thumbnailwidth, m_thumbnailheight; public websitethumbnail(string url, int browserwidth, int browserheight, int thumbnailwidth, int thumbnailheight) { m_url = url; m_browserheight = browserheight; m_browserwidth = browserwidth; m_thumbnailwidth = thumbnailwidth; m_thumbnailheight = thumbnailheight; } public static bitmap getwebsitethumbnail(string url, int browserwidth, int browserheight, int thumbnailwidth, int thumbnailheight) { websitethumbnail thumbnailgenerator = new websitethumbnail(url, browserwidth, browserheight, thumbnailwidth, thumbnailheight); return thumbnailgenerator.generatewebsitethumbnailimage(); } public bitmap generatewebsitethumbnailimage() { thread m_thread = new thread(new threadstart(_generatewebsitethumbnailimage)); m_thread.setapartmentstate(apartmentstate.sta); m_thread.start(); m_thread.join(); return m_bitmap; } private void _generatewebsitethumbnailimage() { webbrowser m_webbrowser = new webbrowser(); m_webbrowser.scrollbarsenabled = false; m_webbrowser.navigate(m_url); m_webbrowser.documentcompleted += new webbrowserdocumentcompletedeventhandler(webbrowser_documentcompleted); while(m_webbrowser.readystate != webbrowserreadystate.complete) application.doevents(); m_webbrowser.dispose(); } private void webbrowser_documentcompleted(object sender, webbrowserdocumentcompletedeventargs e) { webbrowser m_webbrowser = (webbrowser)sender; m_webbrowser.clientsize = new size(this.m_browserwidth, this.m_browserheight); m_webbrowser.scrollbarsenabled = false; m_bitmap = new bitmap(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#程序设计有所帮助。
上一篇: react的滑动图片验证码组件的示例代码
下一篇: C#基于cookie实现的购物车功能