WEB抓網頁形成圖片及前台展現實現原始碼

來源:互聯網
上載者:User

 試用http://121.18.78.216/  希望各位提寶貴意見(自己的討論群152524724),謝謝

 實現思路:

1、通過WebBrower抓取網頁的圖片

2、通過新線程實現ASP.NET使用WinForm組件

3、通過頁面輸出圖片

源碼:

1、通過WebBrower抓取網頁的圖片

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.IO;

/// <summary>                                                
/// GetImage 的摘要說明
/// </summary>
namespace WWW.SnapMap
{
    public class GetImage
    {
        /// <summary>
        /// 寬度
        /// </summary>
        private int ImageWidth;
        /// <summary>
        /// 高度
        /// </summary>
        private int ImageHeight;
        /// <summary>
        /// URL地址
        /// </summary>
        private string WebSite;
        /// <summary>
        /// 構造
        /// </summary>
        /// <param name="webSite"></param>
        /// <param name="imageWidth"></param>
        /// <param name="imageHeight"></param>
        public GetImage(string webSite, int imageWidth, int imageHeight)
        {
            this.WebSite = webSite;
            this.ImageWidth = imageWidth;
            this.ImageHeight = imageHeight;
        }
        /// <summary>
        /// 獲得圖片
        /// </summary>
        /// <returns></returns>
        public byte[] GetBitmap()
        {
            WebPageBitmap Shot = new WebPageBitmap(this.WebSite);
            Shot.GetIt();
            Bitmap Pic = Shot.DrawBitmap(this.ImageWidth, this.ImageHeight);
            return imageToByteArray(Pic);
        }
        /// <summary>
        /// 獲得輸出
        /// </summary>
        /// <param name="bitMap"></param>
        /// <returns></returns>
        private byte[] imageToByteArray(System.Drawing.Bitmap bitMap)
        {
            MemoryStream ms = new MemoryStream();
            bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            return ms.ToArray();
        }
        /// <summary>
        /// 獲得圖片類型
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private System.Drawing.Imaging.ImageFormat getImgFormat(string fileName)
        {
            fileName = Path.GetExtension(fileName).TrimStart('.').ToLower();
            if (fileName.Equals("bmp"))
            {
                return System.Drawing.Imaging.ImageFormat.Bmp;
            }
            if (fileName.Equals("gif"))
            {
                return System.Drawing.Imaging.ImageFormat.Gif;
            }
            if (fileName.Equals("jpg") || fileName.Equals("jpeg"))
            {
                return System.Drawing.Imaging.ImageFormat.Jpeg;
            }
            if (fileName.Equals("png"))
            {
                return System.Drawing.Imaging.ImageFormat.Png;
            }
            if (fileName.Equals("tiff"))
            {
                return System.Drawing.Imaging.ImageFormat.Tiff;
            }
            return System.Drawing.Imaging.ImageFormat.Jpeg;
        }

    }
    /// <summary>
    /// 抓網頁
    /// </summary>
    public class WebPageBitmap
    {
        System.Windows.Forms.WebBrowser MyBrowser;
        string URL;
        public WebPageBitmap(string url)
        {
            this.URL = url;
            MyBrowser = new System.Windows.Forms.WebBrowser();
            MyBrowser.ScrollBarsEnabled = false;
            MyBrowser.Visible = false;
            MyBrowser.Size = new Size(1, 1);
        }

        public void GetIt()
        {
            MyBrowser.Navigate(this.URL, false);
            while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
                System.Threading.Thread.Sleep(1000);
            }
        }

        public Bitmap DrawBitmap(int imgWidth, int imgHeight)
        {
            int DocumentHeight = MyBrowser.Document.Window.Size.Height;
            int DocumentWidth = MyBrowser.Document.Window.Size.Width;
            MyBrowser.Size = new Size(DocumentWidth, DocumentHeight);
            if (DocumentWidth < imgWidth)
            {
                imgWidth = DocumentWidth;
            }
            if (DocumentHeight < imgHeight)
            {
                imgHeight = DocumentHeight;
            }

            Bitmap MyBitmap = new Bitmap(DocumentWidth, DocumentHeight);
            Rectangle DrawRect = new Rectangle(0, 0, DocumentWidth, DocumentHeight);
            /*
             * System.Windows.Forms.WebBrowser繼承了System.Windows.Forms.WebBrowserBase
             * 而
             * System.Windows.Forms.WebBrowserBase繼承了System.Windows.Forms.Control
             *
             * System.Windows.Forms.Control.DrawToBitmap(bitmap,targetBounds)
             * 支援呈現到指定的位元影像
             * bitmap         要繪製到的位元影像。
             * targetBounds   呈現控制項時的邊界。
             *
             * DrawToBitmap 方法具有下列局限性:
             * 可能會針對大位元影像引發 ArgumentException。允許使用的最大大小因電腦而異。
             * DrawToBitmap 不支援 Windows XP Tablet PC Edition 2005 作業系統的 Ink 控制項。
             * 如果 TextBox 的 Visible 屬性設定為 false,則 DrawToBitmap 不繪製子 TextBox。
             * 容器內部的控制項按相反的順序呈現。
             * 對於 RichTextBox,DrawToBitmap 不能完全發揮作用;只繪製位元影像的邊框
             */
            try
            {
                MyBrowser.DrawToBitmap(MyBitmap, DrawRect);
                System.Drawing.Bitmap oThumbNail = new Bitmap(imgWidth, imgHeight, MyBitmap.PixelFormat);
                Graphics g = Graphics.FromImage(oThumbNail);
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                Rectangle oRectangle = new Rectangle(0, 0, imgWidth, imgHeight);
                g.DrawImage(MyBitmap, oRectangle);
                g.Dispose();
                return oThumbNail;
            }
            finally
            {
                MyBitmap.Dispose();
                MyBrowser.Dispose();
                MyBrowser = null;
            }
        }
    }
}

2、通過新線程實現ASP.NET使用WinForm組件

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using MyQuery.Utils;

namespace WWW.SnapMap
{
    public partial class GetMap : System.Web.UI.Page
    {
        private AutoResetEvent autoEvent = new AutoResetEvent(false);

        private byte[] content = null;
        private string url=http://www.baidu.com;

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;

            mapIP = WebHelper.GetAppConfig("GetMapIP");
            ThreadStart start = new ThreadStart(getContent);
            Thread t = new Thread(start);
            t.SetApartmentState(ApartmentState.STA);
            t.Priority = ThreadPriority.Highest;
            t.Start();

            autoEvent.WaitOne(2000, false);
            while (content == null)
            {
                //等待線程結果;
                autoEvent.WaitOne(2000, false);
            }
            autoEvent.WaitOne(2000, false);
            Response.BinaryWrite(content);
            Response.End();
        }

        private void getContent()
        {
            GetImage image = new GetImage(url, 800,600);
            content = image.GetBitmap();
            autoEvent.Set();
        }
    }
}

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetMap.aspx.cs" Inherits="WWW.SnapMap.GetMap" %>

3、通過頁面輸出圖片

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShowMap.aspx.cs" Inherits="WWW.SnapMap.ShowMap" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

</head>
<body oncontextmenu="event.returnValue=false">
    <form id="form1" runat="server">
    <div align="center">
        <img alt="圖片"  src=“GetMap.aspx"  border="0"/>
    </div>
    </form>
</body>
</html>

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.