C#: webfrom 根据网址截图并保存

第一步 新建一个aspx页面

<div>
    网址<asp:TextBox ID="TextBox1" runat="server" Width="427px">http://</asp:TextBox>
        <asp:Button ID="bt" runat="server" OnClick="bt_Click" Text="抓取" /><br />
        <br />
        <div id="Preview" runat="server"></div> 
  </div>

  aspx.cs 页面引用 

using System.Windows.Forms;
using System.Drawing;
using System.Threading;

 以下是.cs 页面的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
using System.Drawing.Imaging;
using System.Runtime.InteropServices.ComTypes;
using System.IO;
using System.Drawing; 

private void GetUrlImg()
    {
        System.Drawing.Image img = GetImage(TextBox1.Text);
        string fold = "imagessctp/";
        string physicsFold = Server.MapPath(fold);
        img.Save(physicsFold +"照片1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
    }


    public static System.Drawing.Image GetImage(string webSite)
    {
        WebPageBitmap Shot = new WebPageBitmap(webSite);
        Shot.GetIt();
        System.Drawing.Image Pic = Shot.DrawImage();
        return Pic;
    }

    protected void bt_Click(object sender, EventArgs e)
    {
        Thread NewTh = new Thread(GetUrlImg);
        NewTh.SetApartmentState(ApartmentState.STA);//必须启动单元线程
        NewTh.Start(); 
       
    } 

在项目中新建一个类 WebPageBitmap 以下内容是类的内容

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Threading;
using System.Drawing.Imaging;
using System.IO;

/// <summary>
///WebPageBitmap 的摘要说明
/// </summary>
public class WebPageBitmap
{
    public WebPageBitmap()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    WebBrowser MyBrowser;
    string URL;
    int Height;
    int Width;


    public WebPageBitmap(string url)
    {
        this.URL = url;
        MyBrowser = new WebBrowser();
        MyBrowser.ScrollBarsEnabled = false;
    }


    /// <summary>
    /// 初始化网页
    /// </summary>
    public void GetIt()
    {
        MyBrowser.Navigate(this.URL);
        while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
        {
            Application.DoEvents();
        }


        this.Height = int.Parse(MyBrowser.Document.Body.GetAttribute("scrollHeight"));
        this.Width = int.Parse(MyBrowser.Document.Body.GetAttribute("scrollwidth"));
        MyBrowser.Size = new Size(this.Width, this.Height);
    }


    /// <summary>
    /// 截图
    /// </summary>
    /// <returns></returns>
    public System.Drawing.Image DrawImage()
    {
        int theight = this.Height;
        int twidth = this.Width;
        Bitmap myBitmap = new Bitmap(Width, Height);
        Rectangle DrawRect = new Rectangle(0, 0, Width, Height);
        MyBrowser.DrawToBitmap(myBitmap, DrawRect);


        System.Drawing.Image imgOutput = myBitmap;
        System.Drawing.Image oThumbNail = new Bitmap(twidth, theight, imgOutput.PixelFormat);
        Graphics g = Graphics.FromImage(oThumbNail);
        g.CompositingQuality = CompositingQuality.HighSpeed;
        g.SmoothingMode = SmoothingMode.HighSpeed;
        g.InterpolationMode = InterpolationMode.HighQualityBilinear;
        Rectangle oRectangle = new Rectangle(0, 0, twidth, theight);
        g.DrawImage(imgOutput, oRectangle);
        try
        {
            return oThumbNail;
        }
        catch
        {
            return null;
        }
        finally
        {
            imgOutput.Dispose();
            imgOutput = null;
            MyBrowser.Dispose();
            MyBrowser = null;
        }
    }

}

这是截的图

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值