-1

hello everyone i am writing a program which captures a part of the screen when a button is clicked. Below is my code

**default.aspx**
    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
        CodeBehind="Default.aspx.cs" Inherits="_9marchexp._Default" %>

    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    </asp:Content>
    <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <h2>
            Welcome to ASP.NET!
        </h2>
        <p>
            To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
        </p>
        <p>
            You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
                title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
        </p>
        <p>
            &nbsp;</p>
        <p>
            &nbsp;</p>
        <p>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        </p>
    </asp:Content>

and

default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace _9marchexp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            Rectangle formRect = this.Bounds; // saving information aout your window in formRect <br/>
                       // (height, width, x, y, etc

      using (Bitmap bitm = new Bitmap(formRect.Width, formRect.Height)) // creating new bitmap of the same <br/>
                                       // height and width as our own window
      {
        using (Graphics graphic = Graphics.FromImage(bitm))
        {
          graphic.CopyFromScreen(new Point(formRect.Left, formRect.Top), Point.Empty, formRect.Size);
        }
        bitm.Save("C:\\Users\\xxxx\\Desktop\\myscreenshot.jpg", ImageFormat.Jpeg); // saving the bitmap bile to destination dir. <br/>
                               // with specified name


        }
    }
    }
}

however the thing is when i execute the code an error message is displayed

***'_9marchexp._Default' does not contain a definition for 'Bounds' and no extension method 'Bounds' accepting a first argument of type '_9marchexp._Default' could be found (are you missing a using directive or an assembly reference?)***

How to correct that, can someone help me??

also if i replace this.Bounds with Screen.PrimaryScreen.Bounds the code runs perfectly but the screenshot of the whole screen is taken which i don't want.

nitinvertigo
  • 1,180
  • 4
  • 32
  • 56
  • 1
    There is no screen for the asp.net running process - so what are you trying to get, the administrator desktop background ? or the user background :) - nether is possible. – Aristos Apr 09 '12 at 12:50
  • Hello Aristos, i am trying to get the screenshot of the screen that is being displayed when the button is clicked.Also as i mentioned if i use screen.primary.screen.bounds i get a screenshot of my current desktop – nitinvertigo Apr 09 '12 at 12:55
  • You definely you have mix up what you can do and what not. The only way you can do something similar is to use javascript and this library http://stackoverflow.com/questions/2732488/how-can-i-convert-an-html-element-to-a-canvas-element/6789627#6789627 – Aristos Apr 09 '12 at 12:57
  • The thing i am actually trying to achieve is to take the screenshot of a particular area of the webpage without using any 3rd party applications and save it on the system.I have heard that it can be achieved by modifying graphics.copyfromscreen..but i am unable to understand how to do it. – nitinvertigo Apr 09 '12 at 13:04

1 Answers1

1

It is impossible to capture the screen of the browser, or of the user, or of the server, or of the desktop or what ever, using the code behind.

A solution that read a page and print it to an image can be found here Convert webpage to image from ASP.NET

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • Are you saying that there is no way i can achieve it :( apart from using 3rd party tools i mean??? – nitinvertigo Apr 09 '12 at 13:21
  • @nitinvertigo I am say that there is no way, nether 3rd party tools that run on code behind and can do what you try to do. There is a complicate way to get the screen using javascript, but only if the user do that with the functionality that you provide. – Aristos Apr 09 '12 at 13:37
  • @nitinvertigo can you tell us what you actually try to do here, them maybe I can help more. – Aristos Apr 09 '12 at 13:40
  • thanx for your response...i am trying to make an export to ppt button on a website.The website contains charts.My requirement is that wen i click on that button,the chart that is displayed on the webpage at that time should be exported to a powerpoint slide as an image.Also i am able to capture the whole screen but not the particular chart. – nitinvertigo Apr 09 '12 at 13:52
  • Also the chart that is being displayed is inside an iframe.it also has a html address. So if u can tell me any way to capture the contents of an url as an image that would also fulfill my requirement. – nitinvertigo Apr 09 '12 at 13:55
  • @nitinvertigo The way to see what user see is this: http://stackoverflow.com/questions/2732488/how-can-i-convert-an-html-element-to-a-canvas-element/6789627#6789627 But you can also download the page, render it on browser and save it... – Aristos Apr 09 '12 at 14:01
  • hi Aristos...i have checked your first solution but it still doesnt suit my requirements...but your second solution seems plausible to me..is it possible to do that on a single button click and will i get the image of the website..if so can u guide me how?? – nitinvertigo Apr 09 '12 at 14:06
  • @nitinvertigo http://stackoverflow.com/questions/2715385/convert-webpage-to-image-from-asp-net – Aristos Apr 09 '12 at 15:03