Winform实现将网页生成图片的方法
程序员文章站
2023-12-16 09:58:58
通常浏览器都有将网页生成图片的功能,本文实例讲述了winform实现将网页生成图片的方法。分享给大家供大家参考。具体方法如下:
工具截图如下:
生成后的图片如下:...
通常浏览器都有将网页生成图片的功能,本文实例讲述了winform实现将网页生成图片的方法。分享给大家供大家参考。具体方法如下:
工具截图如下:
生成后的图片如下:
手动填写网站地址,可选择图片类型和保持图片地址,来生成页面的图片,当图片路径未选择时则保存桌面;
具体代码如下:
将html生成图片的类
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.text; using system.windows.forms; using system.drawing.imaging; using system.runtime.interopservices; using system.security; namespace print { public class test { public static bitmap gethtmlimage(uri urlstring, int width) { webbrowser mycontrol = new webbrowser(); mycontrol.size = new size(width, 10); mycontrol.url = urlstring; while (mycontrol.readystate != webbrowserreadystate.complete) { application.doevents(); } mycontrol.height = mycontrol.document.body.scrollrectangle.height + 20; mycontrol.url = urlstring; webcontrolimage.snapshot snap = new webcontrolimage.snapshot(); bitmap myimage = snap.takesnapshot(mycontrol.activexinstance, new rectangle(0, 0, mycontrol.width, mycontrol.height)); mycontrol.dispose(); return myimage; } /// /// webbrowser获取图形 /// private class webcontrolimage { internal static class nativemethods { [structlayout(layoutkind.sequential)] public sealed class tagdvtargetdevice { [marshalas(unmanagedtype.u4)] public int tdsize; [marshalas(unmanagedtype.u2)] public short tddrivernameoffset; [marshalas(unmanagedtype.u2)] public short tddevicenameoffset; [marshalas(unmanagedtype.u2)] public short tdportnameoffset; [marshalas(unmanagedtype.u2)] public short tdextdevmodeoffset; } [structlayout(layoutkind.sequential)] public class comrect { public int left; public int top; public int right; public int bottom; public comrect() { } public comrect(rectangle r) { this.left = r.x; this.top = r.y; this.right = r.right; this.bottom = r.bottom; } public comrect(int left, int top, int right, int bottom) { this.left = left; this.top = top; this.right = right; this.bottom = bottom; } public static nativemethods.comrect fromxywh(int x, int y, int width, int height) { return new nativemethods.comrect(x, y, x + width, y + height); } public override string tostring() { return string.concat(new object[] { "left = ", this.left, " top ", this.top, " right = ", this.right, " bottom = ", this.bottom }); } } [structlayout(layoutkind.sequential)] public sealed class taglogpalette { [marshalas(unmanagedtype.u2)] public short palversion; [marshalas(unmanagedtype.u2)] public short palnumentries; } } public class snapshot { /// /// ?煺? /// /// com 对象 /// 图象大小 /// public bitmap takesnapshot(object punknown, rectangle bmprect) { if (punknown == null) return null; //必须为com对象 if (!marshal.iscomobject(punknown)) return null; //iviewobject 接口 unsafenativemethods.iviewobject viewobject = null; intptr pviewobject = intptr.zero; //内存图 bitmap ppicture = new bitmap(bmprect.width, bmprect.height); graphics hdrawdc = graphics.fromimage(ppicture); //获取接口 object hret = marshal.queryinterface(marshal.getiunknownforobject(punknown), ref unsafenativemethods.iid_iviewobject, out pviewobject); try { viewobject = marshal.gettypedobjectforiunknown(pviewobject, typeof(unsafenativemethods.iviewobject)) as unsafenativemethods.iviewobject; //调用draw方法 viewobject.draw((int)system.runtime.interopservices.comtypes.dvaspect.dvaspect_content, -1, intptr.zero, null, intptr.zero, hdrawdc.gethdc(), new nativemethods.comrect(bmprect), null, intptr.zero, 0); } catch (exception ex) { console.writeline(ex.message); throw ex; } //释放 hdrawdc.dispose(); return ppicture; } } [suppressunmanagedcodesecurity] internal static class unsafenativemethods { public static guid iid_iviewobject = new guid("{0000010d-0000-0000-c000-000000000046}"); [comimport, guid("0000010d-0000-0000-c000-000000000046"), interfacetype(cominterfacetype.interfaceisiunknown)] public interface iviewobject { [preservesig] int draw([in, marshalas(unmanagedtype.u4)] int dwdrawaspect, int lindex, intptr pvaspect, [in] nativemethods.tagdvtargetdevice ptd, intptr hdctargetdev, intptr hdcdraw, [in] nativemethods.comrect lprcbounds, [in] nativemethods.comrect lprcwbounds, intptr pfncontinue, [in] int dwcontinue); [preservesig] int getcolorset([in, marshalas(unmanagedtype.u4)] int dwdrawaspect, int lindex, intptr pvaspect, [in] nativemethods.tagdvtargetdevice ptd, intptr hictargetdev, [out] nativemethods.taglogpalette ppcolorset); [preservesig] int freeze([in, marshalas(unmanagedtype.u4)] int dwdrawaspect, int lindex, intptr pvaspect, [out] intptr pdwfreeze); [preservesig] int unfreeze([in, marshalas(unmanagedtype.u4)] int dwfreeze); void setadvise([in, marshalas(unmanagedtype.u4)] int aspects, [in, marshalas(unmanagedtype.u4)] int advf, [in, marshalas(unmanagedtype.interface)] system.runtime.interopservices.comtypes.iadvisesink padvsink); void getadvise([in, out, marshalas(unmanagedtype.lparray)] int[] paspects, [in, out, marshalas(unmanagedtype.lparray)] int[] advf, [in, out, marshalas(unmanagedtype.lparray)] system.runtime.interopservices.comtypes.iadvisesink[] padvsink); } } } } }
winfrom后台处理方面代码如下
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using system.drawing.imaging; namespace excel文件处理 { public partial class html : form { public html() { initializecomponent(); } private string imageurl = "";//图片地址 private string imagename = "";//图片名称 private void button1_click(object sender, eventargs e) { string htmlurl = this.txt_url.text.trim(); if (htmlurl=="") { messagebox.show("请输入网址"); return; } if (imageurl.trim()=="") { imageurl = @"c:\users\administrator\desktop"; } try { uri ri = new uri(this.txt_url.text); bitmap bit = print.test.gethtmlimage(ri, 1200); imagename = this.txt_name.text.trim();//图片名称 if (imagename != "") { if (imagename.indexof('.') != -1) {//当用户输入图片后缀时,将后缀截取 imagename.substring(0, imagename.lastindexof('.')); } } else imagename = datetime.now.ticks.tostring();//时间名称 switch (this.combobox1.selectedtext) { case "gif": imageurl += "\\" + imagename + ".gif"; break; case "jpg": imageurl += "\\" + imagename + ".jpg"; break; case "png": imageurl += "\\" + imagename + ".png"; break; default: imageurl += "\\" + imagename + ".png"; break; } switch (this.combobox1.selectedtext) { case "gif": bit.save(imageurl, imageformat.gif); break; case "jpg": bit.save(imageurl, imageformat.jpeg); break; case "png": bit.save(imageurl, imageformat.png); break; default: bit.save(imageurl, imageformat.png); break; } bit = null; imageurl = "";//图片地址 imagename = "";//图片名称 messagebox.show("生产成功"); } catch { messagebox.show("网址输入有误!"); return; } } private void button2_click(object sender, eventargs e) { //获取保存路径 if (this.folderbrowserdialog1.showdialog()==dialogresult.ok) { if (this.folderbrowserdialog1.selectedpath.trim()!="") { imageurl = folderbrowserdialog1.selectedpath; this.label6.text = imageurl; } } } } }
希望本文所述对大家的c#程序设计有所帮助。