Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I snipped this code off a website, however...

private void btnSayHello_Click(object sender, RoutedEventArgs e)
        {
            this.txtSayHello.Text = "Jon C...";
            MessageBox.Show("Hello " + txtSayHello.Text);

            //take a screenshot
            int screenWidth = Screen.GetBounds(new Point(0, 0)).Width; 
            int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
//ERROR: Screen does not exist in the current context


            Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
            Graphics gfx = Graphics.FromImage((Image) bmpScreenShot);
//ERROR: Invalid argument is being passed to Graphics.FromImage, and, 
//conversion to (Image) is not accepted by VS2010 (.NET 4)
            gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
//ERROR: Invalid arguments passed to gfx.CopyFromScreen
            bmpScreenShot.Save("test.jpg", ImageFormat.Jpeg);
//ERROR: The name ImageFormat does not exist in the current context.
        }

Sourced from .[^]
Posted

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Drawing.Imaging;
namespace Screenshot
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
            int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
            Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
            Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
            gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
            bmpScreenShot.Save("test.jpg", ImageFormat.Jpeg);
        }
    }
}

Code snipped from
 
Share this answer
 
Comments
NeptuneHACK! 12-Feb-12 10:01am    
my 5
Add:
C#
using System.Drawing.Imaging;

and the last problem goes away.

The others? Will, it may be that you don't have System.Windows.Forms in you list of using files, and you could add it, but I suspect that you don't have it because your application is not Windows Forms - it is Web based.

If so, then no matter what you do, you cannot take a screen shot of the client computer - security will not let you. You can probably take a screen shot of the server computer, and it may even appear to work (i.e. take a screenshot of the client) while you are testing, but when you move to the production system where the server and client are different computers, it will fail. Guaranteed.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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