0

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. 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. I found this code on 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:

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?)

This is the .aspx code:

    <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. Is what I am working possible to convert to ASP.NET/C#?

Thanks in advance.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Kyle G
  • 149
  • 1
  • 3
  • 10
  • PS-I know there is some very basic changes needed to my codebehind to make it run using ASP.NET but I wanted to post the original Windows Form sample not my failed attempts to convert it. – Kyle G Nov 23 '15 at 13:25
  • The codebehind is executed on server side, so it'll probably try to screen of the server with CopyFromScreen function. – Mr_Thorynque Nov 23 '15 at 13:30

1 Answers1

1

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.

WF/WPF can draw directly to the screen, it knows the screen and has access to it. Web servers for web applications don't. They just output the necessary code (HTML and Javascript) to let the browser draw on the screen. 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. There are some ways though to make a screenshot of your page using HTML5 and Canvas. Take a look here: Using HTML5/Canvas/JavaScript to take screenshots.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Yes that's what I found overall in my searches. I also found the same link as you Patrick but the javascript was a bit intimidating for an at best advanced beginner C#/.js developer like myself. Thank you Patrick! – Kyle G Nov 23 '15 at 13:30
  • I agree Patrick. Time to finish my .js/jquery course on Codecademy! Thanks for saving me a lot of frustration. I'd rather get better at .js than learn Windows Forms. – Kyle G Nov 23 '15 at 15:43