C#网页 截图
程序员文章站
2022-04-09 19:41:14
using System.IO; using System.Drawing; using System.Drawing.Imaging; using System.Threading; using System.Windows.Forms; using System; namespace Conso... ......
using system.io; using system.drawing; using system.drawing.imaging; using system.threading; using system.windows.forms; using system; namespace consoleapp1 { public class capturewebpage { public byte[] picbytes; public capturewebpage(string url) { string bmppath =datetime.now.tostring("yyyymmddhhmmssffff")+".bmp"; bitmap bmp = new bitmap(1000,10000); makepicture(url); bmp = bytetoimage(picbytes); bmp.save(bmppath); } private void makepicture(string url) { thread thread = new thread(delegate () { using (webbrowser browser = new webbrowser()) { browser.scrollbarsenabled = true; browser.allownavigation = true; browser.navigate(url); browser.width = 1024; browser.height =10000; browser.documentcompleted += new webbrowserdocumentcompletedeventhandler(documentcompleted); while (browser.readystate != webbrowserreadystate.complete) { system.windows.forms.application.doevents(); } } }); thread.setapartmentstate(apartmentstate.sta); thread.start(); thread.join(); } private void documentcompleted(object sender, webbrowserdocumentcompletedeventargs e) { webbrowser browser = sender as webbrowser; using (bitmap bitmap = new bitmap(browser.width, browser.height)) { browser.drawtobitmap(bitmap, new system.drawing.rectangle(0, 0, browser.width, browser.height)); using (memorystream stream = new memorystream()) { bitmap.save(stream, system.drawing.imaging.imageformat.png); byte[] bytes = stream.toarray(); picbytes = bytes; } } } public static bitmap bytetoimage(byte[] blob) { memorystream mstream = new memorystream(); byte[] pdata = blob; mstream.write(pdata, 0, convert.toint32(pdata.length)); bitmap bm = new bitmap(mstream, false); mstream.dispose(); return bm; } } }