当前位置:网站首页>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);
}
}
}
}
}
}
}
边栏推荐
猜你喜欢
随机推荐
commands out of sync. did you run multiple statements at once
2022 Heilongjiang latest construction eight members (materialman) simulated examination questions and answers
ICMP协议
Realization of basic function of sequence table
History of Web Technology
How to apply for a secondary domain name?
gocv图片读取并展示
Zipkin is easy to use
Minecraft群组服开服
Introduction to anti interception technology of wechat domain name
c语言自定义类型——结构体,位段(匿名结构体,结构体的自引用,结构体的内存对齐)
Qt——如何在QWidget中设置阴影效果
gocv拆分颜色通道
STM32 new project (refer to punctual atom)
Minecraft air Island service
队列的基本概念介绍以及典型应用示例
Gateway is easy to use
Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)
Detailed explanation of NIN network