This is basically an update of an old Remoting example I posted here 10 years ago, this example does the same with WCF.
This VS 2015 solution contains three projects, configured as multi-startup:
- WCF Service Library, includes the interface and implementation for one method, GetScreen, that takes screenshot of desktop and return it as a Stream.
- Server, a Console application that is self-hosting the WCF library, all code is copied from a MSDN article (link in code).
- Client, a Windows Forms application with a service reference to the WCF service. Calls the service method on button click.
For debugging run VS as Administrator, this is necessary for service to register the base url. As alternative the solution can be compiled and set as single-startup project for Client. Then you run the server console manually from Windows Explorer with admin rights (right-click .exe, run as Admin), then run/debug just client. This is also useful if you want to change base url and need to update the service reference in Client project.
- service method returns a Stream for image bytes rather than Image, because an Image object is not suitable for WCF message transmission.
- the BasicHttpBinding in client app.config is modified for larger message size, because images takes more space than default message size.