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, );
MyControl.Url = UrlString;
while (MyControl.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
MyControl.Height = MyControl.Document.Body.ScrollRectangle.Height + ;
MyControl.Url = UrlString;
WebControlImage.Snapshot snap = new WebControlImage.Snapshot();
Bitmap MyImage = snap.TakeSnapshot(MyControl.ActiveXInstance, new Rectangle(, , 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,
-,
IntPtr.Zero,
null,
IntPtr.Zero,
hDrawDC.GetHdc(),
new NativeMethods.COMRECT(bmpRect),
null,
IntPtr.Zero,
);
}
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);
}
}
}
}
}

将html生成图片的类

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, );
ImageName = this.Txt_Name.Text.Trim();//图片名称
if (ImageName != "")
{
if (ImageName.IndexOf('.') != -)
{//当用户输入图片后缀时,将后缀截取
ImageName.Substring(, 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;
}
} }
}
}

winfrom后台处理方面

C#_Winfrom将浏览器生成Image的更多相关文章

  1. 3.JasperReports学习笔记3-在浏览器生成PDF文件

    转自:https://i.cnblogs.com/posts?categoryid=921197 一.新建web工程,导入jasperreports所需的jar包,配置web.xml <serv ...

  2. MVC4中去掉浏览器生成的无关代码方法

    通过增加Web.Config配置,如: <add key="vs:EnableBrowserLink" value="false"/>可以去掉MVC ...

  3. 利用ios safari浏览器生成桌面快捷方式并唤醒app的示例代码

    html 内容: //通过a链接唤醒app  <a href="app约定好的scheme" id="qbt" style="display:n ...

  4. Google Chrome浏览器中如何使用命令

    Google Chrome浏览器中如何使用命令 | 浏览:2974 | 更新:2014-02-23 23:12 | 标签:chrome 1 2 3 分步阅读 Google Chrome浏览器有很多的特 ...

  5. [转]浏览器渲染机制——一定要放在body底部的js引用

    转自:http://blog.csdn.net/u012251421/article/details/50536265 说明: 本文提到的浏览器均是指Chrome. “script标签“指的都是普通的 ...

  6. jquery-qrcode生成二维码

    一.jquery-qrcode jquery-qrcode是一个为浏览器生成二维码的jquery插件.我们很容易将它集成到我们的应用.该插件也可以独立使用,也比较小.它是直接在客户端生成二维码生成.所 ...

  7. 【架构】生成全局唯一ID的3个思路,来自一个资深架构师的总结

    标识(ID / Identifier)是无处不在的,生成标识的主体是人,那么它就是一个命名过程,如果是计算机,那么它就是一个生成过程.如何保证分布式系统下,并行生成标识的唯一与标识的命名空间有着密不可 ...

  8. Windows Server 2003服务器.net4.0+IIS6.0的服务器,IE11浏览器访问的不兼容性

    工作中发生了一件诡异的事情: 程序在Win7+.NET4.0+IIS7.5的服务器部署,IE8和IE11请求时,响应的样式都正常. 但是在美的同事反映说,Windows Server 2003服务器. ...

  9. Tomcat配置HTTPS方式生成安全证书

    在Tomcat 6中配置SSL双向认证是相当容易的,本文将介绍如何使用JDK的keytool来为Tomcat配置双向SSL认证.并实现批量生成证书 系统需求:JDK 5.0Tomcat 6.0.16启 ...

随机推荐

  1. Solving the SQL Server Multiple Cascade Path Issue with a Trigger (转载)

    Problem I am trying to use the ON DELETE CASCADE option when creating a foreign key on my database, ...

  2. SEO-搜索引擎优化

    一.定义 SEO(Search Engine Optimization):汉译为搜索引擎优化.是一种方式:利用搜索引擎的规则提高网站在有关搜索引擎内的自然排名.目的是:为网站提供生态式的自我营销解决方 ...

  3. myeclipse 10配置jboss 7.1.1无法启动Error: Could not create the Java Virtual Machine

    myeclipse 10中配置jboss 7.1.1,多写了个server name,结果死活启动不了.后来,发现了这个细节. 错误显示: 错误源头: 删掉Server name后,可以完美启动 小问 ...

  4. Python Socket传输文件

    发送端可以不停的发送新文件,接收端可以不停的接收新文件. 例如:发送端输入:e:\visio.rar,接收端会默认保存为 e:\new_visio.rar,支持多并发,具体实现如下: 接收端: 方法一 ...

  5. rsync 数据同步

    什么是Rsync? Rsync是一款开源的.快速的.多功能的.可实现全量及增量的本地货远程数据同步备份的优秀工具.适用于unix/linux/windows等多种操作系统平台. Rsync具有可使本地 ...

  6. python基础学习8----文件基本操作

    一.文件的打开,open函数 f = open(file_name,mode)#创建文件对象 打开模式有很多种 1. 'r': 以只读方式打开文件.文件的指针将会放在文件的开头.这是默认模式. 2. ...

  7. Android之间传递数据包

    在Android中 ,我们知道,两个activity之间通讯要用到Intent类,传递简单数据的方式我们也已经知道了.那么,如何在两个activity之间传递数据包呢,这就要用到我们的Bundle类了 ...

  8. yum 私有仓库

    参考地址:https://blog.oldboyedu.com/autodeploy-yum YUM主要用于自动安装.升级rpm软件包,它能自动查找并解决rpm包之间的依赖关系.要成功的使用YUM工具 ...

  9. node学习笔记_03 express框架

    express api地址:http://www.expressjs.com.cn/starter/static-files.html 一.安装依赖 npm i --save express npm ...

  10. Spring boot结合mybatis开发的报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

    错误:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found),经过排查确定是没有找到xml的原因 ...