湖南住房和城乡建设部网站,wordpress注册栏,做一个网站的全部流程,惠州市博罗县建设局网站在Windows登录界面进行截图#xff0c;目前尝试到的以下的截图方法可以截取到图#xff0c;其它的方式要么卡住#xff0c;要么截出来是黑屏的#xff0c;直接上代码#xff1a; /// summary使用Graphics方式捕获桌面截图(效率低于DesktopCapture#xff0c;主要…在Windows登录界面进行截图目前尝试到的以下的截图方法可以截取到图其它的方式要么卡住要么截出来是黑屏的直接上代码 /// summary使用Graphics方式捕获桌面截图(效率低于DesktopCapture主要用户登录界面截图)/summaryinternal class GraphicCapture{private const int Desktopvertres 117;private const int Desktophorzres 118;/// summary当前最新一帧/summaryprivate Bitmap _currentFrame;private Rectangle _currentScreenBounds;private Graphics _graphic;private readonly object _screenLock new object();private Bitmap _originBitmap;private bool _isManualCaptureStop;private PixelFormat _pixelFormat PixelFormat.Bgra32;private Size DesktopSize{get{IntPtr dc GraphicCapture.GetDC(IntPtr.Zero);int deviceCaps1 GraphicCapture.GetDeviceCaps(dc, 117);int deviceCaps2 GraphicCapture.GetDeviceCaps(dc, 118);GraphicCapture.ReleaseDC(IntPtr.Zero, dc);return new Size(deviceCaps2, deviceCaps1);}}[DllImport(user32.dll)]private static extern IntPtr GetDC(IntPtr ptr);[DllImport(gdi32.dll)]private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);[DllImport(User32.dll)]private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);/// summary构造函数/summarypublic GraphicCapture(){this._currentScreenBounds this.GetScreenRectangle(Screen.PrimaryScreen);this._originBitmap this.CreateBitmap();this._graphic Graphics.FromImage((Image) this._originBitmap);}private Rectangle GetScreenRectangle(Screen screen){Rectangle bounds screen.Bounds;return new Rectangle(new Point(bounds.X, bounds.Y), this.DesktopSize);}/// summaryBitmap转byte[]/summary/// param namebitmap/param/// returns/returnsprivate byte[] BitmapToByteArray(Bitmap bitmap){System.Drawing.Imaging.PixelFormat format this.PixelFormatConverter(this.PixelFormat);Rectangle rect new Rectangle(0, 0, bitmap.Width, bitmap.Height);BitmapData bitmapdata bitmap.LockBits(rect, ImageLockMode.ReadOnly, format);int length Math.Abs(bitmapdata.Stride) * bitmap.Height;byte[] destination new byte[length];Marshal.Copy(bitmapdata.Scan0, destination, 0, length);bitmap.UnlockBits(bitmapdata);return destination;}private Bitmap CreateBitmap(){System.Drawing.Imaging.PixelFormat pixelFormat this.PixelFormatConverter(this.PixelFormat);Size desktopSize this.DesktopSize;int width desktopSize.Width;desktopSize this.DesktopSize;int height desktopSize.Height;int format (int) pixelFormat;return new Bitmap(width, height, (System.Drawing.Imaging.PixelFormat) format);}private System.Drawing.Imaging.PixelFormat PixelFormatConverter(PixelFormat customPixelFormat){if (true);System.Drawing.Imaging.PixelFormat pixelFormat;if (customPixelFormat ! PixelFormat.Bgra32){if (customPixelFormat ! PixelFormat.Bgr24)throw new ArgumentOutOfRangeException(PixelFormat, (object) this.PixelFormat, (string) null);pixelFormat System.Drawing.Imaging.PixelFormat.Format24bppRgb;}elsepixelFormat System.Drawing.Imaging.PixelFormat.Format32bppArgb;if (true);return pixelFormat;}/// summary检索最新的桌面图像和关联的元数据/summaryprivate byte[] GetLatestFrameToByte() this.BitmapToByteArray(this.GetLatestFrameToBitmap());/// summary检索最新的桌面图像/summary/// returns/returnsprivate Bitmap GetLatestFrameToBitmap(){lock (this._screenLock){Graphics graphic this._graphic;int x this._currentScreenBounds.X;int y this._currentScreenBounds.Y;Size desktopSize this.DesktopSize;int width1 desktopSize.Width;desktopSize this.DesktopSize;int height1 desktopSize.Height;Size blockRegionSize new Size(width1, height1);graphic.CopyFromScreen(x, y, 0, 0, blockRegionSize);Size newSize;ref Size local ref newSize;desktopSize this.DesktopSize;int width2 (int) ((double) desktopSize.Width * this.Scale);desktopSize this.DesktopSize;int height2 (int) ((double) desktopSize.Height * this.Scale);local new Size(width2, height2);this._currentFrame newSize.Width this._originBitmap.Width newSize.Height this._originBitmap.Height ? this._originBitmap : new Bitmap((Image) this._originBitmap, newSize);}return this._currentFrame;}/// summary捕获图像像素格式默认为BGRA32/summarypublic PixelFormat PixelFormat{get this._pixelFormat;set{this._pixelFormat value;this._originBitmap?.Dispose();this._graphic?.Dispose();this._originBitmap this.CreateBitmap();this._graphic Graphics.FromImage((Image) this._originBitmap);}}/// summary捕获图像缩放大小默认为1.0/summarypublic double Scale { get; set; } 1.0;/// summary新帧捕获事件/summarypublic event EventHandlerCaptureFrame FrameArrived;/// summary开始捕获/summarypublic void StartCapture(){this._isManualCaptureStop false;}/// summary停止捕获/summarypublic void StopCapture(){this._cancellationTokenSource?.Cancel();this._isManualCaptureStop true;}/// summary获取下一帧图像数据/summary/// param namecaptureFrame/param/// returns/returnspublic bool TryGetNextFrame(out CaptureFrame captureFrame){captureFrame (CaptureFrame) null;if (this._isManualCaptureStop)return false;try{byte[] byteArray this.BitmapToByteArray(this.GetLatestFrameToBitmap());Size size new Size((int) ((double) this.DesktopSize.Width * this.Scale), (int) ((double) this.DesktopSize.Height * this.Scale));captureFrame new CaptureFrame(size, this.PixelFormat, byteArray);return true;}catch (Exception ex){return false;}}}操作鼠标键盘等操作可以参考本人另一篇文章使用C#制作可以录制自动化执行Windows操作脚本工具——类似于按键精灵 - log9527 - 博客园 (cnblogs.com)