C#_Winfrom将浏览器生成Image

时间:2021-09-11 19:39:31
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

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. &lbrack;转&rsqb;浏览器渲染机制——一定要放在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服务器&period;net4&period;0&plus;IIS6&period;0的服务器&comma;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. 最清晰的Android多屏幕适配方案

    问题的引入 当您的Android应用即将发布的时候,如果你想让更多的用户去使用你的应用,摆在工程师面前的一个重要问题就是如何让你的应用能在各种各样的终端上运行,这里的各种各样首当其冲的就是不同的屏幕分 ...

  2. Java 程序优化:字符串操作、基本运算方法等优化策略(二)

    五.数据定义.运算逻辑优化 多使用局部变量 调用方法时传递的参数以及在调用中创建的临时变量都保存在栈 (Stack) 里面,读写速度较快. 其他变量,如静态变量.等,都在堆实例变量 (heap) 中创 ...

  3. PHP函数补完 - var&lowbar;export

    var_export() 函数返回关于传递给该函数的变量的结构信息,它和 var_dump() 类似,不同的是其返回的表示是合法的 PHP 代码.var_export必须返回合法的php代码, 也就是 ...

  4. 练习--python中的Queue与多进程(multiprocessing)

    按官方说法: This module is OBSOLETE and is only provided on PyPI to support old projects that still use i ...

  5. Latex笔记-基本布局

    转自https://linhan.blog.ustc.edu.cn/?p=135&cpage=1 目录改用中文标题并且居中 \renewcommand{\contentsname}{\cent ...

  6. Java之多线程

    一.简述进程与线程的关系   说到多线程就不得不说进程,那么什么是进程:简单的来说就是正在进行中的程序. 在windows中,我们可以直观的看到的正在运行中的程序,即进程,如图:

  7. Linux内核策略介绍

      Linux内核策略介绍学习笔记   主要内容 硬件 策略 CPU 进程调度.系统调用.中断 内存 内存管理 外存 文件IO 网络 协议栈 其他 时间管理 进程调度 内核的运行时间 系统启动.中断发 ...

  8. 【转】javascript Object使用Array的方法

    原文: http://www.cnblogs.com/idche/archive/2012/03/17/2403894.html Array.prototype.push push向数组尾部添加一项并 ...

  9. Appium入门示例&lpar;Java&rpar;

    一.使用Eclipse直接创建案例工程 1.打开Eclipse,[File]-->[New]-->[Project] 2.选择[Java Project]-->[Next] 3.输入 ...

  10. Windows上只复制目录结构不复制文件

    xcopy /T /E D:\filetest\FB\BK\bs\ D:\filetest\asdf