如何截取屏幕截图...错误? [英] how to take a screenshot...bugs?

查看:115
本文介绍了如何截取屏幕截图...错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从网站上摘下了这段代码,但是...

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 .[^]

推荐答案

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);
        }
    }
}



添加:
using System.Drawing.Imaging;


最后一个问题就解决了.

其他?可能,可能是您没有using文件列表中的System.Windows.Forms,并且可以添加它,但是我怀疑您没有它,因为您的应用程序不是Windows Forms -它是基于Web的.

如果是这样,那么无论您做什么,都无法对客户端计算机进行截屏,因为安全性不会允许您.您可能可以对服务器计算机进行截屏,甚至在测试时它甚至可以工作(即截取客户端的屏幕快照),但是当您移至服务器和客户端是不同计算机的生产系统时,它会失败.保证.


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.


这篇关于如何截取屏幕截图...错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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