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

时间:2014-01-08 04:56:17

标签: c# web-services screenshot

我用来捕获屏幕并将图像阵列上传到服务器。但是在截图时它给出了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();

特定错误如下:服务无法启动。 System.ComponentModel.Win32Exception(0x80004005):句柄无效    在System.Drawing.Graphics.CopyFromScreen(Int32 sourceX,Int32 sourceY,Int32 destinationX,Int32 destinationY,Size blockRegionSize,CopyPixelOperation copyPixelOperation)    在F:\ Umesh Projects \ ScreenShare \ ScreenShare \ ScreenShare \ ScreenShare.cs中的ScrrenShare.ScreenShare.TraceService():第48行    在F:\ Umesh Projects \ ScreenShare \ ScreenShare \ ScreenShare \ ScreenShare.cs中的ScrrenShare.ScreenShare.OnStart(String [] args):第29行    在System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(对象状态)    请让我知道解决方案。代码在Window窗体应用程序中工作正常,谢谢你。

1 个答案:

答案 0 :(得分:2)

你做了这一步吗?

在管理工具中设置服务的属性 - >服务 - >右键点击您的服务 - >转到属性 - >在“登录”选项卡下,选中“允许与桌面交互”。