当前位置:网站首页>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);
}
}
}
}
}
}
}
边栏推荐
- Kubedm deploys kubernetes v1.23.5 cluster
- Honeypot attack and defense drill landing application scheme
- Qt的拖动事件
- Googlenet network explanation and model building
- [untitled]
- Makefile Fundamentals
- 判断是否是数独
- 文件上传-upload-labs
- Installing Oracle database 19C RAC on Linux
- Don't know mock test yet? An article to familiarize you with mock
猜你喜欢
随机推荐
Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
随笔:RGB图像颜色分离(附代码)
Getting started with k8s: building MySQL with Helm
双向链表的实现(双向链表与单向链表的简单区别联系和实现)
Minecraft air Island service
zipkin 简单使用
Network security - summary and thinking of easy-to-use fuzzy tester
TCP/IP—传输层
Dip1000 runaway
HCIA - application layer
Minecraft插件服开服
Introduction to anti interception technology of wechat domain name
gocv拆分颜色通道
Implementation of bidirectional linked list (simple difference, connection and implementation between bidirectional linked list and unidirectional linked list)
Kubesphere virtualization KSV installation experience
汉诺塔问题的求解与分析
Aneng logistics' share price hit a new low: the market value evaporated by nearly 10 billion yuan, and it's useless for chairman Wang Yongjun to increase his holdings
Openshift container platform community okd 4.10.0 deployment
Learning C









