public Bitmap GetScreen() { if (State != MirrorState.Connected && State != MirrorState.Running) { throw new InvalidOperationException("In order to get current screen you must at least be connected to the driver"); } PixelFormat format; if (_bitmapBpp == 16) { format = PixelFormat.Format16bppRgb565; } else if (_bitmapBpp == 24) { format = PixelFormat.Format24bppRgb; } else if (_bitmapBpp == 32) { format = PixelFormat.Format32bppArgb; } else { Debug.Fail("Unknown pixel format"); throw new Exception("Unknown pixel format"); } Bitmap result = new Bitmap(_bitmapWidth, _bitmapHeight, format); System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, _bitmapWidth, _bitmapHeight); BitmapData bmpData = result.LockBits(rect, ImageLockMode.WriteOnly, format); // Get the address of the first line. IntPtr ptr = bmpData.Scan0; // Declare an array to hold the bytes of the bitmap. int bytes = bmpData.Stride * _bitmapHeight; GetChangesBuffer getChangesBuffer = (GetChangesBuffer)Marshal.PtrToStructure(_getChangesBuffer, typeof(GetChangesBuffer)); byte[] data = new byte[bytes]; try { Marshal.Copy(getChangesBuffer.UserBuffer, data, 0, bytes); } catch (AccessViolationException) { } // Copy the RGB values into the bitmap. Marshal.Copy(data, 0, ptr, bytes); result.UnlockBits(bmpData); return(result); }
public byte[] GetScreenBuffer() { GetChangesBuffer getChangesBuffer = (GetChangesBuffer)Marshal.PtrToStructure(_getChangesBuffer, typeof(GetChangesBuffer)); byte[] data = new byte[_bitmapWidth * _bitmapHeight * 4]; try { Marshal.Copy(getChangesBuffer.UserBuffer, data, 0, data.Length); } catch (AccessViolationException) { } return(data); }