【发布时间】:2012-04-21 20:06:49
【问题描述】:
大家好,我正在编写一个程序,该程序会在单击按钮时捕获屏幕的一部分。下面是我的代码
**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&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
</p>
<p>
</p>
<p>
</p>
<p>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</p>
</asp:Content>
和
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
}
}
}
}
但问题是当我执行代码时会显示错误消息
***'_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?)***
如何纠正,有人可以帮助我吗??
如果我将 this.Bounds 替换为 Screen.PrimaryScreen.Bounds 代码运行完美,但截取了整个屏幕的屏幕截图,这是我不想要的。
【问题讨论】:
-
asp.net 运行进程没有屏幕 - 那你想得到什么,管理员桌面背景?或用户背景:) - 下界是可能的。
-
你好 Aristos,我正在尝试获取单击按钮时显示的屏幕截图。另外,正如我提到的,如果我使用 screen.primary.screen.bounds,我会得到一个截图我当前的桌面
-
你明确地混淆了你能做什么和不能做什么。您可以做类似事情的唯一方法是使用 javascript 和这个库 stackoverflow.com/questions/2732488/…
-
我实际上想要实现的是在不使用任何第三方应用程序的情况下截取网页特定区域的屏幕截图并将其保存在系统上。我听说可以通过以下方式实现修改 graphics.copyfromscreen ..但我无法理解如何去做。
标签: asp.net screenshot buttonclick