简体   繁体   English

截屏不在窗口服务中工作c#

[英]Taking screenshot not working in window service c#

I am using to capture the screen and upload the image array to server. 我用来捕获屏幕并将图像阵列上传到服务器。 but its giving the win32 exception when taking screenshot, my code is as: 但是在截图时它给出了win32异常,我的代码如下:

            private static Bitmap bmpScreenshot;
            private static Graphics gfxScreenshot;
            bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height ,PixelFormat.Format32bppArgb);

            gfxScreenshot = Graphics.FromImage(bmpScreenshot);

            gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bmpScreenshot.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] bmpBytes = ms.GetBuffer();
            bmpScreenshot.Dispose();
            ms.Close();

The specific error coming as: Service cannot be started. 具体错误如下:服务无法启动。 System.ComponentModel.Win32Exception (0x80004005): The handle is invalid at System.Drawing.Graphics.CopyFromScreen(Int32 sourceX, Int32 sourceY, Int32 destinationX, Int32 destinationY, Size blockRegionSize, CopyPixelOperation copyPixelOperation) at ScrrenShare.ScreenShare.TraceService() in F:\\Umesh Projects\\ScreenShare\\ScreenShare\\ScreenShare\\ScreenShare.cs:line 48 at ScrrenShare.ScreenShare.OnStart(String[] args) in F:\\Umesh Projects\\ScreenShare\\ScreenShare\\ScreenShare\\ScreenShare.cs:line 29 at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state) Please let me know solution.The code is working fine in Window form application Thanks in adv. System.ComponentModel.Win32Exception(0x80004005):在ScrrenShare.ScreenShare.TraceService()中的System.Drawing.Graphics.CopyFromScreen(Int32 sourceX,Int32 sourceY,Int32 destinationX,Int32 destinationY,Size blockRegionSize,CopyPixelOperation copyPixelOperation)处理句柄无效:\\ Umesh Projects \\ ScreenShare \\ ScreenShare \\ ScreenShare \\ ScreenShare.cs:F:\\ Umesh Projects \\ ScreenShare \\ ScreenShare \\ ScreenShare \\ ScreenShare.cs中的ScrrenShare.ScreenShare.OnStart(String [] args)第48行:系统第29行.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(对象状态)请让我知道解决方案。代码在Window窗体应用程序中运行正常感谢adv。

Did you do this step? 你做了这一步吗?

Set the properties of the service in administrative tools -> services -> right click your service -> goto properties -> under Log On tab check allow to interact with desktop. 在管理工具中设置服务的属性 - >服务 - >右键单击您的服务 - >转到属性 - >在“登录”选项卡下选中“允许与桌面交互”。

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

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