c# gdi+截图桌面, 通过remoting发送给wpf展现,即远程监控桌面

1.截图关键代码

 public class ScreenShotHelper : MarshalByRefObject, IScreenShotHelper
    { 
        public byte[] GetImage()
        {
            
            var wid = Screen.PrimaryScreen.Bounds.Width; 
            var hei = Screen.PrimaryScreen.Bounds.Height; 
            MemoryStream ms = new MemoryStream();
            using (Bitmap bitmap = new Bitmap(wid, hei))
            {
                const int SRCCOPY = 13369376;
                const int BLACKONWHITE = 0x0001;
                const int WHITEONBLACK = 0x0002;
                const int COLORONCOLOR = 0x0003;
                const int HALFTONE = 0x0004;
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    // Get a device context to the windows desktop and our destination  bitmaps
                    int hdcSrc = Win32.GetDC(0);
                    IntPtr hdcDest = g.GetHdc();

                    // Copy what is on the desktop to the bitmap
                    //   BitBlt(hdcDest.ToInt32(), 0, 0, wid, hei, hdcSrc, 0, 0, SRCCOPY);
                    int x = Win32.SetStretchBltMode(hdcDest.ToInt32(), COLORONCOLOR);
                    Win32.StretchBlt(hdcDest.ToInt32(), 0, 0, wid, hei, hdcSrc, 0, 0, 960, 1080, SRCCOPY);
                    // Release device contexts
                    g.ReleaseHdc(hdcDest);
                    Win32.ReleaseDC(0, hdcSrc);

                    bitmap.Save(ms, ImageFormat.Jpeg);
                }
            }
            var bt = ms.GetBuffer();
            ms.Dispose();
            return bt;
        } 
    }

2.播放代码

public class DrawingImage : FrameworkElement
    {
        private ImageSource _img;

        public ImageSource Img
        {
            get => _img; set
            {
                _img = value;
                InvalidateVisual();
            }
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);

             drawingContext.DrawImage(Img, new Rect(0, 0, this.ActualWidth, this.ActualHeight));
        }
    }

3.remoting服务

 TcpServerChannel channel = new TcpServerChannel(12345);
            ChannelServices.RegisterChannel(channel);
            //注册远程对象

            RemotingConfiguration.RegisterWellKnownServiceType(
                typeof(ScreenShotHelper),
                "GetScreenShot",
                WellKnownObjectMode.SingleCall);

4.remoting客户端

 //链接远程服务器管道
            TcpClientChannel channel = new TcpClientChannel();
            ChannelServices.RegisterChannel(channel);   
             _remote = (IScreenShotHelper) Activator.GetObject(
              typeof(IScreenShotHelper), "tcp://localhost:12345/GetScreenShot");

效果图

源码:

服务:https://files.cnblogs.com/files/chlm/CSharp%E6%88%AA%E5%9B%BE%E6%9C%8D%E5%8A%A1%E5%99%A8.rar

客户端:https://files.cnblogs.com/files/chlm/Wpf%E5%9B%BE%E7%89%87%E6%92%AD%E6%94%BE%E5%99%A8.rar

接口:https://files.cnblogs.com/files/chlm/%E5%9B%BE%E7%89%87%E6%9C%8D%E5%8A%A1%E5%92%8C%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%8E%A5%E5%8F%A3.rar

原文地址:https://www.cnblogs.com/congqiandehoulai/p/12727279.html