Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I made a website in Asp.net and I've added a code which allows me to screen capture a webpage. I've used the code from this website tutorial 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, the error is with p.start in the private void shot part of the code.

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 5-May-11 6:30am
v2
Comments
Karthik. A 5-May-11 12:25pm    
what's the error?
programmer1234 5-May-11 12:29pm    
It says it can't find a file and it marks the p.start part of the code as the error.

1 solution

Look like your EXE file cannot be found in the location that it is looking.

Are you sure "~\IECapt.exe" is actually where your EXE is?
 
Share this answer
 
Comments
programmer1234 5-May-11 12:51pm    
I download the Iecapt.exe file from the website and it has the file, but the folder name is different. Would I need to change Iecapt.exe in the const string to the folder name?

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