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, );
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);
}
}
}
}
}
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, );
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;
}
} }
}
}
Winform将网页生成图片的更多相关文章
- C# Winform利用POST传值方式模拟表单提交数据(Winform与网页交互)
其原理是,利用winfrom模拟表单提交数据.将要提交的參数提交给网页,网页运行代码.得到数据.然后Winform程序将网页的全部源码读取下来.这样就达到windows应用程序和web应用程序之间传參 ...
- winform里面网页显示指定内容
今天有个同事问了一下我,怎么在winform里面打开网页啊?我们都是基于C/S的开发,很少接触winform,所以我当时就懵了,实在不知道怎么回答,所以索性说不知道.但是我又想了想,这个应该是个很简单 ...
- C# Html网页生成图片解决方案1
1.使用System.Windows.Forms命名空间下的WebBrowser控件加载网页并生成图片 GiHub参考地址: https://github.com/tianma3798/FileOpa ...
- 解决 winform打开网页 和WebBrowser打开链接360误报拦截的问题
以下方法我已经在自己电脑上验证通过,其他电脑并未测试,请广大读者自行验证并反馈,如果有更好的方法请指教. 在winform中如果使用这种方法弹出网页,例如这样 Process.start(" ...
- php中网页生成图片的方式
在网上找了很多方法,发现与自己最初的思路也是大同小异,那就是HTML——>PDF——>JPG.从上午9点钟一直搞到下午6点钟,代码方面其实很简单,更多的还是环境和PHP拓展上面,忙了一天的 ...
- c# winform实现网页上用户自动登陆,模拟网站登录
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...
- winform下载网页源码
public partial class Form1 : Form{public Form1(){InitializeComponent();} private void button1_Click( ...
- winform下载网页代码
1:webClient client=new WebClient(); client.Downloadstring(地址) client.Downloadfile(地址,保存路径) 2:后台线程dow ...
- 网页转图片,html生成图片,网页生成图片(基于linnux+phantomjs)和wkhtmltoimage
安装扩展: (1)下面是我在linux上的安装过程,如果没有安装git请先yum install git 安装casperjs cd / git clone git://githu ...
随机推荐
- 重构15-Remove Duplication(删除重复)
这大概是处理一个方法在多处使用时最常见的重构.如果不加以注意的话,你会慢慢地养成重复的习惯.开发者常常由于懒惰或者在想要尽快生成尽可能多的代码时,向代码中添加很多重复的内容.我想也没必要过多解释了吧, ...
- LearnMVC5-GettingStarted
原创文章,转载必需注明出:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/learnmvc5-gettingstarted/ 本人是 ...
- python 基础——常用功能片段
1. 元素去重 data = [1,2,3,4,4,5,5,6] res = set(data) 2. 元素去重排序 res = sorted(set(data)) 2. 打印重复的元素 res = ...
- hdu 3111 DLX解数独
思路:裸的DLX解数独.关键是建图,感觉还不如写个dfs直接,DLX写这个的代码很烦. #include<set> #include<map> #include<cmat ...
- C之文件读写
1.fopen() fopen的原型是:FILE *fopen(const char *filename,const char *mode),fopen实现三个功能:为使用而打开一个流,把一个文件和此 ...
- html背景自动移动
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- NodeJS学习之文件操作
NodeJS -- 文件操作 Buffer(数据块) JS语言自身只有字符串数据类型,没有二进制数据类型,因此NodeJS提供了一个与String对等的全局构造函数Buffer来提供对二进制数据的操作 ...
- Unity3d 引擎原理详细介绍、Unity3D引擎架构设计
体系结构 为了更好地理解游戏的软件架构和对象模型,它获得更好的外观仅有一名Unity3D的游戏引擎和编辑器是非常有用的,它的主要原则. Unity3D 引擎 Unity3D的是一个屡获殊荣的工具,用于 ...
- UILabel自适应高、宽
根据Label和字体大小自适应高度 - (CGFloat)getHeightWithLabel:(UILabel *)label andFontSize:(CGFloat)size { label.n ...
- apache commons-email1.3使用
apache commons-email1.3下载地址: https://repository.apache.org/content/repositories/orgapachecommons-0 ...