简体   繁体   English

将Windows窗体屏幕截图应用程序转换为ASP.NET

[英]Convert Windows Form Screenshot Application To ASP.NET

I am a C# ASP.NET developer who is trying to create a web app that allows users to designate an area of their screen and screen capture it. 我是一位C#ASP.NET开发人员,他试图创建一个Web应用程序,允许用户指定屏幕区域并对其进行屏幕捕获。 I have found many javascript and jquery solutions but they are too complicated in my opinion and I lack the javascript skills to work with them. 我发现了很多javascript和jquery解决方案,但我认为它们太复杂了,我缺乏与它们一起使用的javascript技能。 I found this code on MSDN: 我在MSDN上找到了以下代码:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;

public class Form1 :
    Form
{
    private Button printButton = new Button();
    private PrintDocument printDocument1 = new PrintDocument();

    public Form1()
    {
        printButton.Text = "Print Form";
        printButton.Click += new EventHandler(printButton_Click);
        printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
        this.Controls.Add(printButton);
    }

    void printButton_Click(object sender, EventArgs e)
    {
        CaptureScreen();
        printDocument1.Print();
    }


    Bitmap memoryImage;

    private void CaptureScreen()
    {
        Graphics myGraphics = this.CreateGraphics();
        Size s = this.Size;
        memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
    }

    private void printDocument1_PrintPage(System.Object sender,
           System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(memoryImage, 0, 0);
    }



    public static void Main()
    {
        Application.Run(new Form1());
    }
}

I put the code in code behind in ASP.NET and added a reference to WindowsFormIntegration to my project but when I run it I get an error: 我将代码放在ASP.NET中的代码中,并在项目中添加了对WindowsFormIntegration的引用,但是在运行它时出现错误:

CS1061: 'ASP.default2_aspx' does not contain a definition for 'printButton_Click' and no extension method 'printButton_Click' accepting a first argument of type 'ASP.default2_aspx' could be found (are you missing a using directive or an assembly reference?) CS1061:“ ASP.default2_aspx”不包含“ printButton_Click”的定义,并且找不到扩展方法“ printButton_Click”接受类型为“ ASP.default2_aspx”的第一个参数(您是否缺少using指令或程序集引用?)

This is the .aspx code: 这是.aspx代码:

    <body bgcolor="Silver">
    <form id="form1" runat="server">
    <br />
    <h2 style="color: #808000; font-size: x-large; font-weight: bolder;">
        Article by Vithal Wadje</h2><br />
        <asp:Label style="color: #808000;" ID="Label1" runat="server" Text="Label"></asp:Label>
    <br />
    <div>

        <br />
      <asp:Button 
            ID="printButton" runat="server"
             onclick="printButton_Click"  />
    </div>
    </form>`enter code here`
</body>
</html>

I'm unfamiliar with Windows Forms so am unsure if I should abandon using this MSDN sample https://msdn.microsoft.com/en-us/library/fw1kt6f9.aspx and look at Silverlight possibly though I'm not familiar there either. 我不熟悉Windows窗体,因此不确定是否应该放弃使用此MSDN示例https://msdn.microsoft.com/zh-cn/library/fw1kt6f9.aspx ,尽管我对此不太熟悉,但还是可以看一下Silverlight 。 Is what I am working possible to convert to ASP.NET/C#? 我正在做的工作可以转换为ASP.NET/C#吗?

Thanks in advance. 提前致谢。

It is not possible to convert a WPF/Windows Forms application to a web application at once since they use a totally different way to communicate with the screen. 由于WPF / Windows窗体应用程序使用完全不同的方式与屏幕进行通信,因此无法将其立即转换为Web应用程序。

WF/WPF can draw directly to the screen, it knows the screen and has access to it. WF / WPF可以直接绘制到屏幕上,它知道屏幕并可以访问它。 Web servers for web applications don't. Web应用程序的Web服务器没有。 They just output the necessary code (HTML and Javascript) to let the browser draw on the screen. 他们只是输出必要的代码(HTML和Javascript)以使浏览器在屏幕上绘制。 The server (where your current code runs) as no way to read the screen. 服务器(您当前代码所在的服务器)无法读取屏幕。

For security reasons it is also impossible to get the screen from the client side from a web application, so you can't get out of your own site's sandbox in the browser. 出于安全原因,也无法从Web应用程序从客户端获取屏幕,因此您无法脱离浏览器中自己站点的沙箱。 There are some ways though to make a screenshot of your page using HTML5 and Canvas. 尽管有一些方法可以使用HTML5和Canvas对页面进行截图。 Take a look here: Using HTML5/Canvas/JavaScript to take screenshots . 在这里看看: 使用HTML5 / Canvas / JavaScript截屏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM