public static void UpdateLayeredWindow(IntPtr hwnd, Point location, Bitmap bitmap, byte alpha)
 {
     try
     {
         BLENDFUNCTION blend = new BLENDFUNCTION();
         blend.BlendOp             = AlphaChannelFlags.AC_SRC_OVER;
         blend.BlendFlags          = 0;
         blend.AlphaFormat         = AlphaChannelFlags.AC_SRC_ALPHA;
         blend.SourceConstantAlpha = alpha;
         POINT  srcLocation    = new POINT();
         POINT  dstLocation    = POINT.FromPoint(location);
         SIZE   dstSize        = SIZE.FromSize(bitmap.Size);
         IntPtr hdc            = WinUserApi.GetWindowDC(hwnd);
         IntPtr bmpDc          = WinGdiApi.CreateCompatibleDC(hdc);
         IntPtr hBitmap        = bitmap.GetHbitmap(Color.FromArgb(0));
         IntPtr originalBitmap = WinGdiApi.SelectObject(bmpDc, hBitmap);
         WinUserApi.UpdateLayeredWindow(hwnd, IntPtr.Zero, ref dstLocation, ref dstSize, bmpDc, ref srcLocation, 0, ref blend, UpdateLayeredWindowFlags.ULW_ALPHA);
         WinGdiApi.SelectObject(bmpDc, originalBitmap);
         WinGdiApi.DeleteObject(hBitmap);
         WinGdiApi.DeleteDC(bmpDc);
         WinUserApi.ReleaseDC(hwnd, hdc);
     }
     catch
     {
     }
 }
        public static Rectangle GetRgnBox(IntPtr hRegion)
        {
            RECT rect;

            WinGdiApi.GetRgnBox(hRegion, out rect);
            return(rect.ToRectangle());
        }
        public static Bitmap PrintWindow(IntPtr hwnd)
        {
            RECT windowRect;

            WinUserApi.GetWindowRect(hwnd, out windowRect);
            IntPtr   hdc    = WinUserApi.GetWindowDC(hwnd);
            Bitmap   bmp    = new Bitmap(windowRect.Width, windowRect.Height, PixelFormat.Format32bppArgb);
            Graphics gfxBmp = Graphics.FromImage(bmp);

            gfxBmp.FillRectangle(new SolidBrush(Color.FromArgb(0, 0, 0, 0)), new Rectangle(Point.Empty, bmp.Size));
            IntPtr hdcBitmap = gfxBmp.GetHdc();
            bool   succeeded = WinUserApi.PrintWindow(hwnd, hdcBitmap, 0);

            gfxBmp.ReleaseHdc(hdcBitmap);
            if (!succeeded)
            {
                gfxBmp.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(Point.Empty, bmp.Size));
            }
            IntPtr hRgn = WinGdiApi.CreateRectRgn(0, 0, 0, 0);

            WinUserApi.GetWindowRgn(hwnd, hRgn);
            Region region = Region.FromHrgn(hRgn);

            if (!region.IsEmpty(gfxBmp))
            {
                gfxBmp.ExcludeClip(region);
                gfxBmp.Clear(Color.FromArgb(0, 0, 0, 0));
            }
            region.Dispose();
            WinGdiApi.DeleteObject(hRgn);
            gfxBmp.Dispose();
            WinUserApi.ReleaseDC(hwnd, hdc);
            return(bmp);
        }
        public static unsafe RECT[] RectsFromRegion(IntPtr hRgn)
        {
            RECT[] rects = new RECT[0];

            // First we call GetRegionData() with a null buffer.
            // The return from this call should be the size of buffer
            // we need to allocate in order to receive the data.
            int dataSize = WinGdiApi.GetRegionData(hRgn, 0, IntPtr.Zero);

            if (dataSize != 0)
            {
                IntPtr bytes = IntPtr.Zero;

                // Allocate as much space as the GetRegionData call
                // said was needed
                bytes = Marshal.AllocCoTaskMem(dataSize);

                // Now, make the call again to actually get the data
                int retValue = WinGdiApi.GetRegionData(hRgn, dataSize, bytes);

                // From here on out, we have the data in a buffer, and we
                // just need to convert it into a form that is more useful
                // Since pointers are used, this whole routine is 'unsafe'
                // It's a small sacrifice to make in order to get this to work.
                // [RBS] Added missing second pointer identifier
                RGNDATAHEADER *header = (RGNDATAHEADER *)bytes;

                if (header->iType == 1)
                {
                    rects = new RECT[header->nCount];

                    // The rectangle data follows the header, so we offset the specified
                    // header size and start reading rectangles.
                    int rectOffset = header->dwSize;
                    for (int i = 0; i < header->nCount; i++)
                    {
                        // simple assignment from the buffer to our array of rectangles
                        // will give us what we want.
                        rects[i] = *((RECT *)((byte *)bytes + rectOffset + (Marshal.SizeOf(typeof(RECT)) * i)));
                    }
                }
            }

            // Return the rectangles
            return(rects);
        }