public static Image GetScreenshot(IntPtr hWnd) { IntPtr hdcScreen = WinAPI.GetDC(hWnd); IntPtr hdc = WinAPI.CreateCompatibleDC(hdcScreen); WinAPI.RECT area = new WinAPI.RECT(); WinAPI.GetWindowRect(hWnd, ref area); IntPtr hBitmap = WinAPI.CreateCompatibleBitmap(hdcScreen, area.right - area.left, area.bottom - area.top); WinAPI.SelectObject(hdc, hBitmap); WinAPI.PrintWindow(hWnd, hdc, 0); Image resultOpaque = Image.FromHbitmap(hBitmap); WinAPI.DeleteObject(hBitmap); WinAPI.DeleteDC(hdc); IntPtr hRegion = WinAPI.CreateRectRgn(0, 0, 0, 0); WinAPI.RegionFlags f = (WinAPI.RegionFlags) WinAPI.GetWindowRgn(hWnd, hRegion); Region region = Region.FromHrgn(hRegion); WinAPI.DeleteObject(hRegion); Bitmap result = new Bitmap(resultOpaque.Width, resultOpaque.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(result); g.Clear(Color.Transparent); if (f != WinAPI.RegionFlags.ERROR) g.SetClip(region, System.Drawing.Drawing2D.CombineMode.Replace); if (f != WinAPI.RegionFlags.NULLREGION) g.DrawImageUnscaled(resultOpaque, 0, 0); g.Dispose(); region.Dispose(); resultOpaque.Dispose(); return result; }
public static Bitmap CaptureRuneScapeScreen() { Bitmap image = null; try { PinloggerHelpers.RECT r; WinAPI.GetWindowRect(PinloggerHelpers.curhWnd, out r); Point p = new Point(r.Left, r.Top); int w = r.Right - r.Left; int h = r.Bottom - r.Top; int hdcSrc = WinAPI.GetWindowDC(PinloggerHelpers.curhWnd.ToInt32()), hdcDest = WinAPI.CreateCompatibleDC(hdcSrc), hBitmap = WinAPI.CreateCompatibleBitmap(hdcSrc, w, h); WinAPI.SelectObject(hdcDest, hBitmap); WinAPI.BitBlt(hdcDest, 0, 0, w, h, hdcSrc, 0, 0, 0x00CC0020); image = new Bitmap(Image.FromHbitmap(new IntPtr(hBitmap)), Image.FromHbitmap(new IntPtr(hBitmap)).Width, Image.FromHbitmap(new IntPtr(hBitmap)).Height); Cleanup(hBitmap, hdcSrc, PinloggerHelpers.curhWnd, hdcDest); GC.Collect(); } catch (Exception ex) { Config.DumpErrorLog(ex, null); } return(image); }
internal void RefleshLayeredForm() { Bitmap formBitMap = new Bitmap(base.Width, base.Height); Graphics g = Graphics.FromImage(formBitMap); DrawShadow(g); WinAPI.POINT ptSrc = new WinAPI.POINT(0, 0); WinAPI.POINT ptWinPos = new WinAPI.POINT(base.Left, base.Top); WinAPI.SIZE szWinSize = new WinAPI.SIZE(Width, Height); byte biAlpha = 0xFF; WinAPI.BLENDFUNCTION stBlend = new WinAPI.BLENDFUNCTION( (byte)WinAPI.BlendOp.AC_SRC_OVER, 0, biAlpha, (byte)WinAPI.BlendOp.AC_SRC_ALPHA); IntPtr gdiBitMap = IntPtr.Zero; IntPtr memoryDC = IntPtr.Zero; IntPtr preBits = IntPtr.Zero; IntPtr screenDC = IntPtr.Zero; try { screenDC = WinAPI.GetDC(IntPtr.Zero); memoryDC = WinAPI.CreateCompatibleDC(screenDC); gdiBitMap = formBitMap.GetHbitmap(Color.FromArgb(0)); preBits = WinAPI.SelectObject(memoryDC, gdiBitMap); WinAPI.UpdateLayeredWindow(base.Handle , screenDC , ref ptWinPos , ref szWinSize , memoryDC , ref ptSrc , 0 , ref stBlend , (uint)WinAPI.ULWPara.ULW_ALPHA); } finally { if (gdiBitMap != IntPtr.Zero) { WinAPI.SelectObject(memoryDC, preBits); WinAPI.DeleteObject(gdiBitMap); } WinAPI.DeleteDC(memoryDC); WinAPI.ReleaseDC(IntPtr.Zero, screenDC); g.Dispose(); formBitMap.Dispose(); } }
private static void _SetImage(Bitmap image, IntPtr clipboardOwner) { Bitmap tempImage = new Bitmap(image.Width, image.Height); using (Graphics graphics = Graphics.FromImage(tempImage)) { IntPtr hScreenDC = WinAPI.GetWindowDC(IntPtr.Zero); // 기본적인 Device Context의 속성들을 카피하기 위한 작업 IntPtr hDestDC = WinAPI.CreateCompatibleDC(hScreenDC); IntPtr hDestBitmap = WinAPI.CreateCompatibleBitmap(hScreenDC, image.Width, image.Height); // destDC와 destBitmap 모두 반드시 screenDC의 속성들을 기반으로 해야 함. IntPtr hPrevDestObject = WinAPI.SelectObject(hDestDC, hDestBitmap); IntPtr hSourceDC = graphics.GetHdc(); IntPtr hSourceBitmap = image.GetHbitmap(); IntPtr hPrevSourceObject = WinAPI.SelectObject(hSourceDC, hSourceBitmap); WinAPI.BitBlt(hDestDC, 0, 0, image.Width, image.Height, hSourceDC, 0, 0, WinAPI.SRCCOPY); WinAPI.DeleteObject(WinAPI.SelectObject(hSourceDC, hPrevSourceObject)); WinAPI.SelectObject(hDestDC, hPrevDestObject); // 리턴값 : hDestBitmap graphics.ReleaseHdc(hSourceDC); WinAPI.DeleteDC(hDestDC); bool isClipboardOpen = WinAPI.OpenClipboard(clipboardOwner); if (!isClipboardOpen) { WinAPI.DeleteObject(hDestBitmap); WinAPI.DeleteObject(hSourceDC); WinAPI.DeleteObject(hSourceBitmap); tempImage.Dispose(); throw new CannotOpenException(); } WinAPI.EmptyClipboard(); WinAPI.SetClipboardData(WinAPI.CF_BITMAP, hDestBitmap); WinAPI.CloseClipboard(); WinAPI.DeleteObject(hDestBitmap); WinAPI.DeleteObject(hSourceDC); WinAPI.DeleteObject(hSourceBitmap); } tempImage.Dispose(); }
public static Bitmap CaptureScreen() { Bitmap image = null; try { int w = Screen.PrimaryScreen.Bounds.Size.Width; int h = Screen.PrimaryScreen.Bounds.Size.Height; int hdcSrc = WinAPI.GetWindowDC(PinloggerHelpers.curhWnd.ToInt32()), hdcDest = WinAPI.CreateCompatibleDC(hdcSrc), hBitmap = WinAPI.CreateCompatibleBitmap(hdcSrc, w, h); WinAPI.SelectObject(hdcDest, hBitmap); WinAPI.BitBlt(hdcDest, 0, 0, w, h, hdcSrc, 0, 0, 0x00CC0020); image = new Bitmap(Image.FromHbitmap(new IntPtr(hBitmap)), Image.FromHbitmap(new IntPtr(hBitmap)).Width, Image.FromHbitmap(new IntPtr(hBitmap)).Height); Cleanup(hBitmap, hdcSrc, PinloggerHelpers.curhWnd, hdcDest); GC.Collect(); } catch (Exception ex) { Config.DumpErrorLog(ex, null); } return(image); }
public void SetBitmap(Bitmap bitmap) { if (!IsHandleCreated || DesignMode) { return; } IntPtr oldBits = IntPtr.Zero; IntPtr screenDC = WinAPI.GetDC(IntPtr.Zero); IntPtr hBitmap = IntPtr.Zero; IntPtr memDC = WinAPI.CreateCompatibleDC(screenDC); try { Point topLocation = new Point(this.Left, this.Top); Size bitmapSize = new Size(bitmap.Width, bitmap.Height); WinAPI.BLENDFUNCTION blendFunc = new WinAPI.BLENDFUNCTION(); Point sourceLocation = Point.Empty; hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBits = WinAPI.SelectObject(memDC, hBitmap); blendFunc.BlendOp = WinAPI.AC_SRC_OVER; blendFunc.SourceConstantAlpha = 255; blendFunc.AlphaFormat = WinAPI.AC_SRC_ALPHA; blendFunc.BlendFlags = 0; WinAPI.UpdateLayeredWindow(Handle, screenDC, ref topLocation, ref bitmapSize, memDC, ref sourceLocation, 0, ref blendFunc, WinAPI.ULW_ALPHA); } finally { if (hBitmap != IntPtr.Zero) { WinAPI.SelectObject(memDC, oldBits); WinAPI.DeleteObject(hBitmap); } WinAPI.ReleaseDC(IntPtr.Zero, screenDC); WinAPI.DeleteDC(memDC); } }
public static Bitmap GetScreenAreaBitmap(int x, int y, int width, int height, PixelFormat pixelFormat) { var bmp = new Bitmap(width, height); using (var graphics = Graphics.FromImage(bmp)) { #if USE_WINAPI IntPtr hdc_source = IntPtr.Zero; IntPtr hdc_destination = IntPtr.Zero; #if USE_WINAPI_METHOD_I try { hdc_source = WinAPI.GetDC(WinAPI.GetDesktopWindow()); // hdc_source = WinAPI.GetDC(IntPtr.Zero); hdc_destination = graphics.GetHdc(); WinAPI.BitBlt(hdc_destination, 0, 0, width, height, hdc_source, x, y, TernaryRasterOperations.SRCCOPY); } catch { throw; } finally { WinAPI.ReleaseDC(IntPtr.Zero, hdc_source); WinAPI.ReleaseDC(IntPtr.Zero, hdc_destination); WinAPI.DeleteDC(hdc_source); WinAPI.DeleteDC(hdc_destination); graphics.ReleaseHdc(); } #else IntPtr compatible_bitmap_handle = IntPtr.Zero; try { hdc_source = WinAPI.GetDC(WinAPI.GetDesktopWindow()); hdc_destination = WinAPI.CreateCompatibleDC(hdc_source); compatible_bitmap_handle = WinAPI.CreateCompatibleBitmap(hdc_source, width, height); WinAPI.SelectObject(hdc_destination, compatible_bitmap_handle); WinAPI.BitBlt(hdc_destination, 0, 0, width, height, hdc_source, x, y, TernaryRasterOperations.SRCCOPY); } catch { throw; } finally { WinAPI.ReleaseDC(IntPtr.Zero, hdc_source); WinAPI.ReleaseDC(IntPtr.Zero, hdc_destination); WinAPI.DeleteDC(hdc_source); WinAPI.DeleteDC(hdc_destination); WinAPI.DeleteObject(compatible_bitmap_handle); } #endif #else graphics.CopyFromScreen(x, y, 0, 0, bmp.Size); #endif var cursorInfo = new CURSORINFO(); cursorInfo.Initialize(); WinAPI.GetCursorInfo(ref cursorInfo); if (cursorInfo.hCursor != IntPtr.Zero) { var cursor = new Cursor(cursorInfo.hCursor); //IntPtr hdc_source_2 = WinAPI.GetDC(cursor.Handle); //WinAPI.BitBlt(hdc_destination, cursor.HotSpot.X, cursor.HotSpot.Y, cursor.Size.Width, cursor.Size.Height, hdc_source_2, 0, 0, TernaryRasterOperations.SRCPAINT); //WinAPI.ReleaseDC(cursor.Handle, hdc_source_2); //WinAPI.ReleaseDC(IntPtr.Zero, hdc_source_2); //WinAPI.DeleteDC(hdc_source_2); // FIXME - Cursors.IBeam is white var cursorPosition = new Point(Cursor.Position.X - x, Cursor.Position.Y - y); cursor.Draw(graphics, new Rectangle(cursorPosition, new Size(cursor.Size.Width, cursor.Size.Height))); } } Thread.Sleep(10); return(bmp.Clone(new Rectangle(0, 0, bmp.Width, bmp.Height), pixelFormat)); }
public void SetBits() { //绘制绘图层背景 Bitmap bitmap = new Bitmap(Main.Width + 10, Main.Height + 10); Rectangle _BacklightLTRB = new Rectangle(20, 20, 20, 20);//窗体光泽重绘边界 Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.HighQuality; //高质量 g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量 ImageDrawRect.DrawRect(g, shadowimg, ClientRectangle, Rectangle.FromLTRB(_BacklightLTRB.X, _BacklightLTRB.Y, _BacklightLTRB.Width, _BacklightLTRB.Height), 1, 1); // RenderHelper.DrawImageWithNineRect(g, shadowimg, ClientRectangle, new Rectangle { Size = shadowimg.Size }); if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat)) { throw new ApplicationException("图片必须是32位带Alhpa通道的图片。"); } WinAPI.POINT ptSrc = new WinAPI.POINT(0, 0); WinAPI.POINT ptWinPos = new WinAPI.POINT(base.Left, base.Top); WinAPI.SIZE szWinSize = new WinAPI.SIZE(Width, Height); byte biAlpha = 0xFF; WinAPI.BLENDFUNCTION stBlend = new WinAPI.BLENDFUNCTION( (byte)WinAPI.BlendOp.AC_SRC_OVER, 0, biAlpha, (byte)WinAPI.BlendOp.AC_SRC_ALPHA); IntPtr gdiBitMap = IntPtr.Zero; IntPtr memoryDC = IntPtr.Zero; IntPtr preBits = IntPtr.Zero; IntPtr screenDC = IntPtr.Zero; try { screenDC = WinAPI.GetDC(IntPtr.Zero); memoryDC = WinAPI.CreateCompatibleDC(screenDC); gdiBitMap = bitmap.GetHbitmap(Color.FromArgb(0)); preBits = WinAPI.SelectObject(memoryDC, gdiBitMap); WinAPI.UpdateLayeredWindow(base.Handle , screenDC , ref ptWinPos , ref szWinSize , memoryDC , ref ptSrc , 0 , ref stBlend , (uint)WinAPI.ULWPara.ULW_ALPHA); } finally { if (gdiBitMap != IntPtr.Zero) { WinAPI.SelectObject(memoryDC, preBits); WinAPI.DeleteObject(gdiBitMap); } WinAPI.DeleteDC(memoryDC); WinAPI.ReleaseDC(IntPtr.Zero, screenDC); g.Dispose(); bitmap.Dispose(); } }