public static extern int XShmDetach(System.IntPtr display, ref XShmSegmentInfo shminfo);
public static extern unsafe XImage *XShmCreateImage(System.IntPtr display, Visual *visual, uint depth , int format, System.IntPtr data, ref XShmSegmentInfo shminfo, uint width, uint height);
private static unsafe byte[] UnsafeX11ScreenshotWithCursor(bool withCursor) { byte[] result = null; int AllPlanes = ~0; System.UIntPtr AllPlanes2 = new System.UIntPtr((uint)AllPlanes); System.IntPtr display = LibX11Functions.XOpenDisplay(System.IntPtr.Zero); int defaultScreen = LibX11Functions.XDefaultScreen(display); System.UIntPtr window = LibX11Functions.XRootWindow(display, defaultScreen); XWindowAttributes xa = new XWindowAttributes(); LibX11Functions.XGetWindowAttributes(display, window, ref xa); Screen *screen = xa.screen; // struct screen XShmSegmentInfo shminfo = new XShmSegmentInfo(); XImage *ximg = LibXExt.XShmCreateImage(display, LibXExt.DefaultVisualOfScreen(screen) , (uint)LibXExt.DefaultDepthOfScreen(screen), LinScreen.ZPixmap , System.IntPtr.Zero, ref shminfo, (uint)xa.width, (uint)xa.height); shminfo.shmid = LibC.shmget(LibC.IPC_PRIVATE, new System.IntPtr(ximg->bytes_per_line * ximg->height), LibC.IPC_CREAT | 0777); ximg->data = (sbyte *)LibC.shmat(shminfo.shmid, System.IntPtr.Zero, 0); shminfo.shmaddr = (System.IntPtr)ximg->data; shminfo.readOnly = 0; if (shminfo.shmid < 0) { System.Console.WriteLine("Fatal shminfo error!"); } int s1 = LibXExt.XShmAttach(display, ref shminfo); // System.Console.WriteLine("XShmAttach() {0}\n", s1 != 0 ? "success!" : "failure!"); int res = LibXExt.XShmGetImage(display, window, ximg, 0, 0, AllPlanes2); // const char *filename = "/tmp/test.bmp"; // WriteBitmapToFile(filename, (int) ximg->bits_per_pixel, (int)window_attributes.width, (int)window_attributes.height, (const void*) ximg->data); int bytesPerPixel = (ximg->bits_per_pixel + 7) / 8; int stride = 4 * ((ximg->width * bytesPerPixel + 3) / 4); // long size = ximg->height * stride; // byte[] managedArray = new byte[size]; // Marshal.Copy((IntPtr)(ximg->data), managedArray, 0, (int)size); // System.Console.WriteLine(managedArray); if (withCursor) { PaintMousePointer(display, ximg); } // End if (withCursor) using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ximg->width, ximg->height, stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, (System.IntPtr)ximg->data)) { // bmp.Save("/tmp/shtest.bmp"); #if false // ZERO compression at all ! // using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) // { // bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); // result = ms.ToArray(); // } // End Using ms #else result = CompressImage(bmp, 25); #endif } // End Using bmp LibX11Functions.XDestroyImage2(ximg); LibXExt.XShmDetach(display, ref shminfo); LibC.shmdt(shminfo.shmaddr); LibX11Functions.XCloseDisplay(display); return(result); } // End Function UnsafeX11ScreenshotWithCursor