ASP.NETは、URLに応じてページのサムネイルサンプルプログラム(C#言語)を生成します

仕事はすぐに予め用意されたURLに基​​づいて、ページのサムネイルを生成するために使用することができます。

インターネット上のソースを手に入れたが、エラーがあります:現在のスレッドがこのバックアップに記録されて良い実行後に解決し、シングルスレッドアパートメント、それが不可能なActiveXコントロールをインスタンス化すること「8856f961-340a-11D0-A9」、ではありません!

ホーム:Default.aspxを

ます。<%@ Page Language = "C#の" AutoEventWireup属性= "true" を分離コード= "Default.aspx.cs" 継承= "CaptureToImage._Default" %>

<!DOCTYPE htmlののPUBLIC " - // W3C // DTD XHTML 1.0過渡// EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<HTMLのxmlns =」http://www.w3.org/1999/xhtml ">
<ヘッドID =" HEAD1"にrunat = "サーバー">
  <タイトル>スナップ</タイトル>
</ head>
<body>
  <FORM ID = "Form1の"にrunat = "サーバー">
  の<div>
  の<input type = "ボタン" onclickの= "window.open( 'Snap.aspx?URL = www.njude.com.cn')"値= "生成网页缩略图"/>
  </ div>
  </ FORM>
</ BODY>
</ HTML>

呼び出しページ:Snap.aspx

ます。<%@ Page Language = "C#" AutoEventWireup属性= "true"を分離コード= "Snap.aspx.csは、" = "CaptureToImage.Snap"継承ASPCOMPAT = "真"%>

<!DOCTYPE htmlののPUBLIC " - // W3C // DTD XHTML 1.0過渡// EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<HTMLのxmlns =」http://www.w3.org/1999/xhtml ">
<ヘッドにrunat ="サーバー">
    <タイトル>无标题页</タイトル>
</ head>
<body>
    <フォームID =" Form1"のにrunat = "サーバー">
    の<div>
   
    </ div>
    </ FORM>
</ BODY>
</ HTML>

PS:赤いフォントがシングルスレッド環境で実行するプログラムを強制、エラーを解決するために追加されたコードの一部です!

呼び出しページ:Snap.aspx.cs

システムを使用しました。
System.Dataを使用しました。
System.Configurationを使用しました。
System.Collectionsを使用しました。
System.Webのを使用して、
System.Web.Securityを使用しました。
たSystem.Web.UIを使用しました。
System.Web.UI.WebControlsを使用しました。
System.Web.UI.WebControls.WebPartsを使用しました。
System.Web.UI.HtmlControlsを使用しました。
System.Drawing.Imagingを使用しました。

名前空間CaptureToImage
{
    パブリック部分クラススナップ:System.Web.UI.Page
    {
        保護のボイドをPage_Load(オブジェクト送信者、EventArgsの電子)
        {
            文字列のURL = String.Emptyを。
            URL =場合、Request.QueryString [0]。
            試す
            {
                のgetImage親指=新しいのgetImage(URL、1024、768、800、600);
                System.Drawing.Bitmap X = thumb.GetBitmap()。
                x.Save(Response.OutputStream、ImageFormat.Jpeg)。
                Response.ContentTypeのは= "画像/ JPEG"。
            }
            キャッチ(例外例)
            {
                のResponse.Write(ex.Message)。
            }
        }
    }
}

クラスファイル:GetImage.cs

システムを使用しました。
System.Drawingを使用しました。
System.Drawing.Imagingを使用しました。
System.Windows.Formsを使用しました。
たSystem.Web.UIを使用しました。

名前空間CaptureToImage
{
    publicクラスのgetImage
    {
        int型S_Height。
        int型S_Width;
        int型F_Height;
        int型F_Width;
        文字列MyURL;

        公共int型ScreenHeight
        {
            取得
            {
                S_Heightを返します。
            }
            集合
            {
                S_Height =値。
            }
        }

        公共int型ScreenWidth
        {
            取得
            {
                S_Widthを返します。
            }
            集合
            {
                S_Width =値。
            }
        }

        公共int型のImageHeight
        {
            取得
            {
                F_Heightを返します。
            }
            集合
            {
                F_Height =値。
            }
        }

        公共int型のImageWidth
        {
            取得
            {
                F_Widthを返します。
            }
            集合
            {
                F_Width =値。
            }
        }

        公共の文字列のウェブサイト
        {
            取得
            {
                MyURLを返します。
            }
            集合
            {
                MyURL =値。
            }
        }

        公共のgetImage(ストリングウェブサイト、INT ScreenWidth、INT ScreenHeight、INT ImageWidth、INT ImageHeight)
        {
            this.WebSite =ウェブサイト。
            this.ScreenHeight = ScreenHeight。
            this.ScreenWidth = ScreenWidth。
            this.ImageHeight = ImageHeight。
            this.ImageWidth = ImageWidth。
        }
        [STAThread]
        パブリックビットマップGetBitmap()
        {
            WebPageBitmapショット=新しいWebPageBitmap(this.WebSite、this.ScreenWidth、this.ScreenHeight)。

            Shot.GetIt();
            ビットマップピック= Shot.DrawBitmap(this.ImageHeight、this.ImageWidth)。
            ピックを返します。
        }
    }

    パブリッククラスWebPageBitmap
    {
        WebブラウザーMYBROWSER。
        文字列のURL。
        int型身長;
        int型の幅;

        公共WebPageBitmap(文字列のURL、INT幅、int型の高さ)
        {
            this.URL = URL。
            this.Width =幅;
            this.Height =高さ;
            MYBROWSER =新しいWebBrowserコントロール();
            = falseをMyBrowser.ScrollBarsEnabled。
            MyBrowser.Size =新しいサイズ(this.Width、this.Height)。
        }

        公共ボイドGetIt()
        {
            NavigateUrl(this.URL)。
            (!MyBrowser.ReadyState = WebBrowserReadyState.Complete)一方
            {
                Application.DoEvents();
            }
        }

        パブリックデリゲートのボイドDelUserHandler(文字列のURL);

        公共のボイドNavigateUrl(文字列のURL)
        {
            しようと
            {
                (this.MyBrowser.InvokeRequired)場合
                {
                    DelUserHandlerハンドラは=新しいDelUserHandler(NavigateUrl)。
                    MyBrowser.Invoke(ハンドラ、URL);
                }
                他
                {
                    this.MyBrowser.Navigate(URL)。
                }
            }
            キャッチ(例外例)
            {
                新しい例外( "NavigateUrl()" + ex.Message)を投げます。
            }
        }
        パブリックビットマップDrawBitmap(INT theight、INT twidth)
        {
            ビットマップmyBitmap =新しいビットマップ(this.Width、this.Height)。
            矩形のdrawRect =新しいRectangle(0、0、this.Width、this.Height)。
            MyBrowser.DrawToBitmap(myBitmap、のdrawRect)。
            System.Drawing.Image imgOutput = myBitmap。
            System.Drawing.Bitmap oThumbNail =新しいビットマップ(twidth、theight、imgOutput.PixelFormat)。
            グラフィック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。

            矩形oRectangle =新しいRectangle(0、0、twidth、theight)。
            g.DrawImage(imgOutput、oRectangle)。

            試す
            {
                oThumbNailを返します。
            }
            キャッチ
            {
                戻りヌル。
            }
            最後に
            {
                imgOutput.Dispose()。
                imgOutput = NULL;
                MyBrowser.Dispose();
                MYBROWSER = NULL;
            }

        }
    }
}

PS:このプロジェクトは、のSystem.Windows.Formsへの参照を追加する必要があります

ます。https://www.cnblogs.com/guoxiaowen/archive/2012/02/16/2353900.htmlで再現

おすすめ

転載: blog.csdn.net/weixin_34326179/article/details/93329999