1. 程式人生 > >ASP.NET根據URL生成網頁縮圖示例程式(C#語言)

ASP.NET根據URL生成網頁縮圖示例程式(C#語言)

工作中可能馬上要用到根據URL生成網頁縮圖功能,提前做好準備。

在網上找了份原始碼,但是有錯誤:當前執行緒不在單執行緒單元中,因此無法例項化 ActiveX 控制元件“8856f961-340a-11d0-a9”,解決後執行良好,記錄在此備用!

起始頁:Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CaptureToImage._Default" %>

<html xmlns="http://www.w3.org/1999/xhtml

" >
<head id="Head1" runat="server">
  <title>Snap</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <input type="button" onclick="window.open('Snap.aspx?url=www.njude.com.cn')" value="生成網頁縮圖"/>
  </div>
  </form>
</body>
</html>

呼叫頁:Snap.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Snap.aspx.cs" Inherits="CaptureToImage.Snap" AspCompat="true" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>無標題頁</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

PS:紅色字型部分是為解決錯誤增加的程式碼,強制程式在單執行緒環境下執行!

呼叫頁:Snap.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;

namespace CaptureToImage
{
    public partial class Snap : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string url = string.Empty;
            url = Request.QueryString[0];
            try
            {
                GetImage thumb = new GetImage(url, 1024, 768, 800, 600);
                System.Drawing.Bitmap x = thumb.GetBitmap();
                x.Save(Response.OutputStream, ImageFormat.Jpeg);
                Response.ContentType = "image/jpeg";
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}

類檔案:GetImage.cs

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Web.UI;

namespace CaptureToImage
{
    public class GetImage
    {
        int S_Height;
        int S_Width;
        int F_Height;
        int F_Width;
        string MyURL;

        public int ScreenHeight
        {
            get
            {
                return S_Height;
            }
            set
            {
                S_Height = value;
            }
        }

        public int ScreenWidth
        {
            get
            {
                return S_Width;
            }
            set
            {
                S_Width = value;
            }
        }

        public int ImageHeight
        {
            get
            {
                return F_Height;
            }
            set
            {
                F_Height = value;
            }
        }

        public int ImageWidth
        {
            get
            {
                return F_Width;
            }
            set
            {
                F_Width = value;
            }
        }

        public string WebSite
        {
            get
            {
                return MyURL;
            }
            set
            {
                MyURL = value;
            }
        }

        public GetImage(string WebSite, int ScreenWidth, int ScreenHeight, int ImageWidth, int ImageHeight)
        {
            this.WebSite = WebSite;
            this.ScreenHeight = ScreenHeight;
            this.ScreenWidth = ScreenWidth;
            this.ImageHeight = ImageHeight;
            this.ImageWidth = ImageWidth;
        }
        [STAThread]
        public Bitmap GetBitmap()
        {
            WebPageBitmap Shot = new WebPageBitmap(this.WebSite, this.ScreenWidth, this.ScreenHeight);

            Shot.GetIt();
            Bitmap Pic = Shot.DrawBitmap(this.ImageHeight, this.ImageWidth);
            return Pic;
        }
    }

    public class WebPageBitmap
    {
        WebBrowser MyBrowser;
        string URL;
        int Height;
        int Width;

        public WebPageBitmap(string url, int width, int height)
        {
            this.URL = url;
            this.Width = width;
            this.Height = height;
            MyBrowser = new WebBrowser();
            MyBrowser.ScrollBarsEnabled = false;
            MyBrowser.Size = new Size(this.Width, this.Height);
        }

        public void GetIt()
        {
            NavigateUrl(this.URL);
            while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }
        }

        public delegate void DelUserHandler(string url);

        public void NavigateUrl(string url)
        {
            try
            {
                if (this.MyBrowser.InvokeRequired)
                {
                    DelUserHandler handler = new DelUserHandler(NavigateUrl);
                    MyBrowser.Invoke(handler, url);
                }
                else
                {
                    this.MyBrowser.Navigate(url);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("NavigateUrl()" + ex.Message);
            }
        }
        public Bitmap DrawBitmap(int theight, int twidth)
        {
            Bitmap myBitmap = new Bitmap(this.Width, this.Height);
            Rectangle DrawRect = new Rectangle(0, 0, this.Width, this.Height);
            MyBrowser.DrawToBitmap(myBitmap, DrawRect);
            System.Drawing.Image imgOutput = myBitmap;
            System.Drawing.Bitmap oThumbNail = new Bitmap(twidth, theight, imgOutput.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, twidth, theight);
            g.DrawImage(imgOutput, oRectangle);

            try
            {
                return oThumbNail;
            }
            catch
            {
                return null;
            }
            finally
            {
                imgOutput.Dispose();
                imgOutput = null;
                MyBrowser.Dispose();
                MyBrowser = null;
            }

        }
    }

}

我用了這個GetImage來實現定期從模板把aspxnavigate為html,然後儲存為三個圖片。

相關推薦

ASP.NET根據URL生成網頁示例程式(C#語言)

工作中可能馬上要用到根據URL生成網頁縮圖功能,提前做好準備。 在網上找了份原始碼,但是有錯誤:當前執行緒不在單執行緒單元中,因此無法例項化 ActiveX 控制元件“8856f961-340a-11d0-a9”,解決後執行良好,記錄在此備用! 起始頁:Default

java根據url生成網頁

public class ShotsPic extends JPanel { private static final long serialVersionUID = 1L; // 行分隔符 final static public String LS = Syste

Asp.net Mvc Framework 一 (安裝並建立示例程式)

Asp.net Mvc 是微軟官方提供的Mvc模式編寫Asp.netWeb應用程式的一個框架,它由Castle的MonoRail而來.目前已經歷經數個版本 通過上面兩個網站可以獲取AspNetMVc的安裝檔案. 下載後將會獲得一個AspNetMVCPreviewX-setup.msi的安裝檔案

Asp.Net Core MVC控制器和視之間傳值

view 指定 mode 設置 http adg nbsp urn 傳值方式 一、Core MVC中控制器和視圖之間傳值方式和Asp.Net中非常類似 1.弱類型數據:ViewData,ViewBag 2.強類型數據:@model 二、代碼 實例 1.ViewData

如何應用ASP.NET MVC中的分部視

如何 重復 shtml 技術 選擇 partial viewbag body html 概述:   在ASP.NET Web Form的開發經驗中,對於User Control使用比較頻繁,可以減少重復的代碼,利於頁面模塊化,這個概念也被引入了ASP.NET M

ASP.NET MVC傳遞Model到視的多種方式總結

ajax tco layout reac demo con png 轉換成 sin ASP.NET MVC傳遞Model到視圖的多種方式總結 有多種方式可以將數據傳遞到視圖,如下所示: ViewData ViewBag PartialView TempDat

C# .NET 根據Url鏈接保存Image圖片到本地磁盤

blank mar 取圖 查看 本地 文件 content C# 文件中 原文:C# .NET 根據Url鏈接保存Image圖片到本地磁盤根據一個Image的Url鏈接可以在瀏覽器中顯示一個圖片,如果要通過代碼將圖片保存在本地磁盤可以通過以下方式: 1、首先獲取圖片的二進

ASP.NET Core中使用Razor視引擎渲染視為字符串(轉)

http onf ces mod ado efault his .html 返回 一、視圖渲染說明 在有些項目需求上或許需要根據模板生產靜態頁面,那麽你一樣可以用Razor語法去直接解析你的頁面從而把解析的頁面生成靜態頁,這樣的使用場景很多,不限於生成靜態頁面,視圖引擎為我

beetl學習根據URL生成模板

字符輸入 apr ner ioe .get catch muti puts () 官方文檔: http://ibeetl.com/guide/#beetl 多謝beetl的作者抽空指點!!! 根據遠程文件服務器生成模板: 需要註意的是: StringTemplateRes

JAVA生成圖片、JAVA擷取圖片區域性內容

目前,google已經有了更好的處理JAVA圖片的工具,請搜尋:Thumbnailator    package com.ares.image.test; import java.awt.Color; import java.awt.Graphics; impor

Java——使用javacv生成視訊

轉載大佬文章,以備後期再次需要,親測使用有效 新增依賴 在pom.xml中新增依賴配置 <dependencies>   <dependency>     <groupId>org.bytedeco</groupId

asp.net根據引數找不到記錄後響應404及顯示錯誤頁

在asp.net mvc 中,action方法里根據引數獲取資料,假如獲取的資料為空,為了響應404錯誤頁,我們可以return HttpNotFound(); 但是在asp.net webform中,實現方式就不一樣了。 為了體現本人在實現過程中的所遇到的問題,現舉例來說明。 1. 在asp.net w

無搜尋條件根據url獲取網頁資料(java爬取網頁資料)

jsoup jar包 <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.11.3<

Spring MVC上傳圖片,Java二進位制圖片寫入資料庫,生成

步驟:1.將圖片上傳到伺服器的一個磁碟目錄下。 2.將剛才上傳好的圖片寫入資料庫image欄位。 一、上傳圖片:使用的是spring mvc 對上傳的支援。 jsp 頁面: <form name="uploadForm" id="uploadForm" m

JAVA生成圖片、JAVA擷取圖片區域性內容的案例

JAVA生成圖片縮圖 package com.ares.image.test; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.image.Bu

ASP.NET MVC URL Routing 學習

定義URL Routing 在一個route中,通過在大括號中放一個佔位符來定義( { and } )。當解析URL的時候,符號"/"和"."被作為一個定義符來解析,而定義符之間的值則匹配到佔位符中。route定義中不在大括號中的資訊則作為常量值。下面是一些示例URL:

asp.net中ashx生成驗證碼程式碼放在Linux(centos)主機上訪問時無法顯示問題

最近有個專案加入了驗證碼功能,就從自己部落格以前的程式碼中找到直接使用,直接訪問驗證碼頁面報錯如下: Application Exception System.ArgumentException The requested FontFamily could not be found [GDI+

PHP 生成圖片函式

各位小盆友使用前記得開啟 GD 庫的支援哦,附上程式碼。 <?php /** * 生成縮圖函式(支援圖片格式:gif、jpeg、png和bmp) * @author ruxing.li * @param string $src 源圖片路徑 * @p

php根據URL獲得網頁內容

php 中根據url來獲得網頁內容非常的方便,可以通過系統內建函式file_get_contents(),傳入url,即可返回網頁的內容,比如獲得百度首頁的內容程式碼為:<?php $html = file_get_contents('http://www.baidu.

讀取24位 BMP 影象並生成 JPG (一)

            //對24位BMP進行解析     if(nbitcount==24){         int npad=(nsizeimage/nheight)-nwidth*3;         int ndata[]=new int[nheight*nwidth];         byte