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 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); }