Example #1
0
        private static unsafe byte[] SlowScreenshotWithCursor(
            System.IntPtr display
            , System.UIntPtr d
            , int x, int y
            , uint width
            , uint height
            , System.UIntPtr plane_mask
            , int format, bool withCursor)
        {
            byte[] result = null;

            XImage *img = LibX11Functions.XGetImage2(display, d, x, y, width, height, plane_mask, format);

            if (withCursor)
            {
                PaintMousePointer(display, img);
            } // End if (withCursor)


            int bitsPerPixel = img->bits_per_pixel;

            System.UIntPtr ptrImg         = (System.UIntPtr)img;
            System.UIntPtr pixels         = (System.UIntPtr)(&(img->data));
            System.UIntPtr ptr_byte_order = (System.UIntPtr)(&(img->byte_order));

            ulong int64  = pixels.ToUInt64() - ptrImg.ToUInt64();
            ulong int642 = ptr_byte_order.ToUInt64() - ptrImg.ToUInt64();



            System.Console.WriteLine("p1: {0}, p2: {1}", ptrImg.ToUInt64(), pixels.ToUInt64());
            System.Console.WriteLine("Delta: {0}", int64);    // 16
            System.Console.WriteLine("Delta 2: {0}", int642); // 16

            // https://stackoverflow.com/questions/30476131/c-sharp-read-pointer-address-value
            System.IntPtr ptr       = (System.IntPtr)(ptrImg.ToUInt64() + 16);
            long          longValue = System.Runtime.InteropServices.Marshal.ReadInt64(ptr);



            System.Console.WriteLine("pt1: {0}, pt2: {1}", ((System.UIntPtr)(img->data)).ToUInt64(), longValue);


            // BMPImage * foo = CreateBitmapFromScan0(uint16_t bitsPerPixel, int32_t w, int32_t h, uint8_t* scan0);
            // string filename = "/tmp/lol1.bmp";
            // WriteBitmapToFile(filename, bitsPerPixel, width, height, );

            System.Console.WriteLine("Format: {0}, bpp: {1}", format, bitsPerPixel);

            int bytesPerPixel = (bitsPerPixel + 7) / 8;
            int stride        = 4 * (((int)width * bytesPerPixel + 3) / 4);

            // long size = height * stride;
            // byte[] managedArray = new byte[size];
            // System.Runtime.InteropServices.Marshal.Copy((System.IntPtr)(img->data), managedArray, 0, (int)size);

            using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap((int)width, (int)height, stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, (System.IntPtr)img->data))
            {
                // bmp.Save("/tmp/lol1.bmp");

                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    result = ms.ToArray();
                } // End Using ms
            }     // End Using bmp

            LibX11Functions.XDestroyImage2(img);

            return(result);
        }
Example #2
0
        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