Example #1
0
        // ###############################################################################
        // ### M E T H O D S
        // ###############################################################################

        #region Methods

        /// <summary>Get the image as System.Drawing.Bitmap.</summary>
        /// <returns>The image as bitmap on success, or null otherwise.<see cref="System.Drawing.Bitmap"/></returns>
        public System.Drawing.Bitmap GetBitmap()
        {
            if (_imageSurface == null || _imageSurface.Drawable == IntPtr.Zero)
            {
                return(null);
            }

            IntPtr image = X11lib.XGetImage(_imageSurface.Display, _imageSurface.Drawable,
                                            0, 0, (X11.TUint)_size.Width, (X11.TUint)_size.Height,
                                            (X11.TUlong)(UInt32.MaxValue), (X11.TInt) 2);

            if (image == IntPtr.Zero)
            {
                return(null);
            }

            System.Drawing.Bitmap bmp   = new System.Drawing.Bitmap(_size.Width, _size.Height);
            System.Drawing.Color  color = System.Drawing.Color.Black;
            for (int scanLine = 0; scanLine < _size.Height; scanLine++)
            {
                for (int scanCol = 0; scanCol < _size.Width; scanCol++)
                {
                    X11.TPixel pixel = X11lib.XGetPixel(image, (X11.TInt)scanCol, (X11.TInt)scanLine);
                    if (_imageSurface.Depth >= 24)
                    {
                        color = System.Drawing.Color.FromArgb((int)pixel);
                    }
                    else
                    {
                        color = System.Drawing.Color.FromArgb(_imageSurface.RgbForColor(pixel));
                    }
                    bmp.SetPixel(scanCol, scanLine, color);
                }
            }

            X11lib.XDestroyImage(image);
            return(bmp);
        }
        // ###############################################################################
        // ### D E S T R U C T I O N
        // ###############################################################################

        #region Destruction

        /// <summary> IDisposable implementation. </summary>
        public void Dispose()
        {
            // Console.WriteLine (CLASS_NAME + "::Dispose ()");

            if (_graphicXImage != IntPtr.Zero)
            {
                // Note: The destroy procedure (_XImage.f.destroy_image), that this macro calls,
                // frees both - the image structure and the data pointed to by the image structure.
                X11lib.XDestroyImage(_graphicXImage);
                _graphicXImage = IntPtr.Zero;
            }
            if (_transpXImage != IntPtr.Zero)
            {
                // Note: The destroy procedure (_XImage.f.destroy_image), that this macro calls,
                // frees both - the image structure and the data pointed to by the image structure.
                X11lib.XDestroyImage(_transpXImage);
                _transpXImage = IntPtr.Zero;
            }
            if (_transpXPixmap != IntPtr.Zero)
            {
                X11lib.XFreePixmap(_display, _transpXPixmap);
                _transpXPixmap = IntPtr.Zero;
            }
        }