C#_Winfrom将浏览器生成Image
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的更多相关文章
- 3.JasperReports学习笔记3-在浏览器生成PDF文件
转自:https://i.cnblogs.com/posts?categoryid=921197 一.新建web工程,导入jasperreports所需的jar包,配置web.xml <serv ...
- MVC4中去掉浏览器生成的无关代码方法
通过增加Web.Config配置,如: <add key="vs:EnableBrowserLink" value="false"/>可以去掉MVC ...
- 利用ios safari浏览器生成桌面快捷方式并唤醒app的示例代码
html 内容: //通过a链接唤醒app <a href="app约定好的scheme" id="qbt" style="display:n ...
- Google Chrome浏览器中如何使用命令
Google Chrome浏览器中如何使用命令 | 浏览:2974 | 更新:2014-02-23 23:12 | 标签:chrome 1 2 3 分步阅读 Google Chrome浏览器有很多的特 ...
- [转]浏览器渲染机制——一定要放在body底部的js引用
转自:http://blog.csdn.net/u012251421/article/details/50536265 说明: 本文提到的浏览器均是指Chrome. “script标签“指的都是普通的 ...
- jquery-qrcode生成二维码
一.jquery-qrcode jquery-qrcode是一个为浏览器生成二维码的jquery插件.我们很容易将它集成到我们的应用.该插件也可以独立使用,也比较小.它是直接在客户端生成二维码生成.所 ...
- 【架构】生成全局唯一ID的3个思路,来自一个资深架构师的总结
标识(ID / Identifier)是无处不在的,生成标识的主体是人,那么它就是一个命名过程,如果是计算机,那么它就是一个生成过程.如何保证分布式系统下,并行生成标识的唯一与标识的命名空间有着密不可 ...
- Windows Server 2003服务器.net4.0+IIS6.0的服务器,IE11浏览器访问的不兼容性
工作中发生了一件诡异的事情: 程序在Win7+.NET4.0+IIS7.5的服务器部署,IE8和IE11请求时,响应的样式都正常. 但是在美的同事反映说,Windows Server 2003服务器. ...
- Tomcat配置HTTPS方式生成安全证书
在Tomcat 6中配置SSL双向认证是相当容易的,本文将介绍如何使用JDK的keytool来为Tomcat配置双向SSL认证.并实现批量生成证书 系统需求:JDK 5.0Tomcat 6.0.16启 ...
随机推荐
- 转:asp.net mvc下的多语言方案 包含Html,Javascript和图片
可以不使用微软的Resource文件,而是将所有的词汇放入在一个txt的词典之中,便于维护. 步骤如下: 1)在整个程序的入口处global.asax.cs加入函数 private void Read ...
- 判断Exception类中是否有InnerException属性
public static class ExceptionExtend { /// <summary> /// 利用反射来判断对象是否包含某个属性 /// </summary> ...
- 华为MSTP负载均衡配置示例
以下内容摘自由华为公司授权并审核通过,今年元月刚刚出版上市的<华为交换机学习指南>一书:http://item.jd.com/11355972.html,http://product.da ...
- 1 什么是virtual Machine
1.所有的虚拟机以文件的形式存放在存储上. 2.虚拟机的文件构成: swap files: <vm_name>.vswp 虚拟机的内存文件,vmx-<vm_name>.vsw ...
- fedora27安装后的配置工作(持续更新)
换源 没什么可说的,安装后更换国内软件源是必须做的事,推荐更换阿里的镜像源.换源教程 添加epel源 EPEL (Extra Packages for Enterprise Linux)是基于Fedo ...
- Amazon Redshift数据库
Amazon Redshift介绍 Amazon Redshift是一种可轻松扩展的完全托管型PB级数据仓库,它通过使用列存储技术和并行化多个节点的查询来提供快速的查询性能,使您能够更高效的分析现有数 ...
- .Net Core Api 跨域配置
.Net Core 和Asp.Net 不同,不需要再去引用其他的跨域组件.创建项目时,就有了. 让接口实现跨域,需要配置两个地方. 一.Startup.cs 这里需要配置两个地方 public voi ...
- 设置webstorm支持ES6语法
1. 点击File目录下的Default Settings 2. 再依次点击Languages & Frameworks -----> JaveScript ----> ...
- JavaScript中数组slice和splice的对比小结
前言 今天重温了一下Javascript,看到了数组的方法,其中有两个比较相似的方法——splice和splice,看着很像,就是多了一个p,但是用法却相当不一样. 在使用中,可以通过选择一个具有强语 ...
- Azkaban学习之路 (二)Azkaban的安装
安装过程 1.软件介绍 Azkaban Web 服务器:azkaban-web-server-2.5.0.tar.gz Azkaban Excutor 执行服务器:azkaban-executor-s ...