如何使用Asp.net屏蔽网页? [英] How can I screen cap a web page using Asp.net?

查看:100
本文介绍了如何使用Asp.net屏蔽网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在asp.net中制作了一个网页,我想知道如何使用asp.net来对网页进行屏幕截图,asp中是否有控件可以让我做到这一点,或者我需要手动创建它?

我已经使用了来自此网站 http://ramanisandeep.wordpress.com/2009/12/05/capture-web-page-as-image-using-asp-net/ [

I''ve made a webpage in asp.net and I was wondering how can I screen cap a webpage using asp.net, is there a control in asp that allows me to do this or would I need to manually create it?

I''ve used the code from this website http://ramanisandeep.wordpress.com/2009/12/05/capture-web-page-as-image-using-asp-net/[^] but when I run the program it comes up with an error.

Heres my code, i''ve marked where it said the error was.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["Path"] != null)
        {
            theImage.ImageUrl = "~//" + Request.QueryString["Path"].ToString();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }
       public class CaptureWebPage
    {
        private const string EXTRACTIMAGE_EXE = "IECapt.exe";
        private const int TIMEOUT = 60000;
        private const string TMP_NAME = "InBetween.png";

        public CaptureWebPage()
        {
        }

        private void Shot(string url, string rootDir)
        {
            Process p = new Process();
            p.StartInfo.FileName = rootDir + "\\" + EXTRACTIMAGE_EXE;
            p.StartInfo.Arguments = String.Format("\"{0}\" \"{1}\"", url, rootDir + "\\" + TMP_NAME);
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = false;
            p.Start(); //this is where the error comes up           
            p.WaitForExit();
            p.Dispose();
        }

        private System.Drawing.Image Scale(System.Drawing.Image imgPhoto, int Width, int Height)
        {
            int srcWidth = imgPhoto.Width;
            int srcHeight = imgPhoto.Height;
            int srcX = 0; int srcY = 0;
            int destX = 0; int destY = 0;

            float percent = 0; float percentWidth = 0; float percentHeight = 0;
            percentWidth = ((float)Width / (float)srcWidth);
            percentHeight = ((float)Height / (float)srcHeight);

            if (percentHeight < percentWidth)
            {
                percent = percentWidth;
                destY = 0;
            }
            else
            {
                percent = percentHeight;
                destX = 0;
            }

            int destWidth = (int)(srcWidth * percent);
            int destHeight = (int)(srcHeight * percent);

            System.Drawing.Bitmap bmPhoto = new System.Drawing.Bitmap(Width,
                    Height, PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                   imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.InterpolationMode =
                    InterpolationMode.HighQualityBicubic;

            grPhoto.DrawImage(imgPhoto,
                new Rectangle(destX, destY, destWidth, destHeight),
               new Rectangle(srcX, srcY, srcWidth, srcHeight),
               GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return bmPhoto;
        }

        public string GetImage(string url, string name, string rootDir, int width, int height)
        {
            string fileName = rootDir + "\\" + TMP_NAME;
            Shot(url, rootDir);
            System.Drawing.Image thumbImage = System.Drawing.Image.FromFile(fileName);
            Scale(thumbImage, width, height);
            System.Drawing.Image scaledImg = Scale(thumbImage, width, height);
            fileName = rootDir + "\\" + name + ".png";
            if (File.Exists(fileName))
                File.Delete(fileName);
            scaledImg.Save(fileName, ImageFormat.Png);
            return name + ".png";
        }
    }

       protected void btnCapture_Click(object sender, EventArgs e)
    {
        int Width, Height;
        Width = Convert.ToInt32(txtWidth.Text);
        Height = Convert.ToInt32(txtHeight.Text);
        CaptureWebPage cwp = new CaptureWebPage();
        string imagePath = cwp.GetImage(txtUrl.Text, txtDestImage.Text, Server.MapPath("~"), Width, Height);
        Response.Redirect("~/Default2.aspx?Path=" + imagePath);
    }
}

推荐答案

尝试
使用C#捕获整个Web页面 [使用ASP.NET将网页捕获为图像 [ ^ ]
Try
Image Capture Whole Web Page using C#[^]
Capture web page as image using ASP.NET[^]


这篇关于如何使用Asp.net屏蔽网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆