当前位置:网站首页>C# 将网页保存为图片(利用WebBrowser)
C# 将网页保存为图片(利用WebBrowser)
2022-07-02 06:32:00 【清山博客】
using System;
using System.Drawing;
using System.Windows.Forms;
namespace UrlToImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var browser = new WebBrowser {ScrollBarsEnabled = false, ScriptErrorsSuppressed = true};
browser.Navigate("http://www.rc114.com/");
browser.DocumentCompleted += webBrowser_DocumentCompleted;
}
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var browser = (WebBrowser) sender;
if (browser.ReadyState == WebBrowserReadyState.Complete)
{
if (browser.Document != null)
{
if (browser.Document.Body != null)
{
var height = browser.Document.Body.ScrollRectangle.Height;
var width = browser.Document.Body.ScrollRectangle.Width;
browser.Height = height;
browser.Width = width;
using (var bitmap = new Bitmap(width, height))
{
var rectangle = new Rectangle(0, 0, width, height);
browser.DrawToBitmap(bitmap, rectangle);
var dialog = new SaveFileDialog();
dialog.Filter = " JPEG (*.jpg)|*.jpg|PNG (*.png)|*.png ";
dialog.ShowDialog();
bitmap.Save(dialog.FileName);
}
}
}
}
}
}
}
边栏推荐
- TCP/IP—传输层
- Linked list classic interview questions (reverse the linked list, middle node, penultimate node, merge and split the linked list, and delete duplicate nodes)
- Dip1000 runaway
- STM32 new project (refer to punctual atom)
- Detailed explanation of NIN network
- sqli-labs(POST类型注入)
- Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
- Network security - summary and thinking of easy-to-use fuzzy tester
- Routing foundation - dynamic routing
- OpenFeign 簡單使用
猜你喜欢
OpenFeign 简单使用
Sqli labs (post type injection)
文件上传-upload-labs
Linux binary installation Oracle database 19C
Comparable,Comparator,Clonable 接口使用剖析
Openfeign is easy to use
File upload Labs
Rotating linked list (illustration)
Kubedm deploys kubernetes v1.23.5 cluster
Getting started with k8s: building MySQL with Helm
随机推荐
Shortcut key to comment code and cancel code in idea
The best blog to explain the basics of compilation (share)
kubeadm部署kubernetes v1.23.5集群
k8s入门:Helm 构建 MySQL
Simple implementation scheme of transcoding and streaming (I)
一、Qt的核心类QObject
Rotating linked list (illustration)
Linux binary installation Oracle database 19C
Data asset management function
PCL calculates the intersection of three mutually nonparallel planes
gocv opencv exit status 3221225785
Don't know mock test yet? An article to familiarize you with mock
OpenShift构建镜像
Qt的右键菜单
Dip1000 runaway
ARP及ARP欺骗
用数字 5,5,5,1 ,进行四则运算,每个数字当且仅当用一次,要求运算结果值为 24
Jumping | Blue Bridge Cup
gocv图片裁剪并展示
Loadbalancer dynamically refreshes Nacos server