Click here to Skip to main content
15,860,861 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

EDIT: 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);
    }
}
Posted
Updated 1-May-11 8:10am
v3

1 solution

 
Share this answer
 
v2
Comments
programmer1234 1-May-11 12:32pm    
I've tried that example before but it doesnt work, it just displays the image as a black screen lol.
RaviRanjanKr 1-May-11 12:34pm    
Updated Check other Links and if you are getting problem then you should post your code too with your question. its will help us to analyze error. :)
thatraja 1-May-11 12:37pm    
Good links Ravi.
RaviRanjanKr 1-May-11 12:40pm    
Thanks Raja :)
programmer1234 1-May-11 14:09pm    
I've used the code from the second link, but it come up with an error, i've put my code in the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900