public SteamGuard(string EmailorPhone, string user) { InitializeComponent(); this.Activate(); this.components.SetStyle(this); this.FormBorderStyle = FormBorderStyle.None; Region = Region.FromHrgn(Gdi32.CreateRoundRectRgn(0, 0, Width, Height, 5, 5)); foreach (var button in this.Controls.OfType <MetroFramework.Controls.MetroButton>()) { IntPtr ptr = Gdi32.CreateRoundRectRgn(1, 1, button.Width, button.Height, 5, 5); button.Region = Region.FromHrgn(ptr); Gdi32.DeleteObject(ptr); } lbl_account.Text = user; if (EmailorPhone == "Phone") { lbl_infoemailorPhone.Text = "Enter your two-factor authentication code"; lbl_emojiInfo.Text = "📱"; } else { lbl_infoemailorPhone.Text = "Enter Steam Guard code from your email"; lbl_emojiInfo.Text = "📧"; MongoToolTip.SetToolTip(lbl_emojiInfo, EmailorPhone); } }
/// <summary> /// Captures a Specific Window. /// </summary> /// <param name="Window">The <see cref="Window"/> to Capture</param> /// <param name="IncludeCursor">Whether to include the Mouse Cursor.</param> /// <returns>The Captured Image.</returns> public static Bitmap Capture(Window Window, bool IncludeCursor = false) { User32.GetWindowRect(Window.Handle, out var r); var region = r.ToRectangle(); IntPtr hSrc = GetWindowDC(Window.Handle), hDest = Gdi32.CreateCompatibleDC(hSrc), hBmp = Gdi32.CreateCompatibleBitmap(hSrc, region.Width, region.Height), hOldBmp = Gdi32.SelectObject(hDest, hBmp); Gdi32.BitBlt(hDest, 0, 0, region.Width, region.Height, hSrc, region.Left, region.Top, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); var bmp = Image.FromHbitmap(hBmp); Gdi32.SelectObject(hDest, hOldBmp); Gdi32.DeleteObject(hBmp); Gdi32.DeleteDC(hDest); Gdi32.DeleteDC(hSrc); var clone = bmp.Clone(new Rectangle(Point.Empty, bmp.Size), PixelFormat.Format24bppRgb); if (IncludeCursor) { new MouseCursor().Draw(clone, region.Location); } return(clone); }
public object Convert(object?value, Type targetType, object parameter, CultureInfo culture) { var guildId = (uint?)value ?? 0; if (!S_IMAGE_DATA.Database.ContainsKey(guildId)) { return(MiscResources.DefaultGuildLogo); } var ip = S_IMAGE_DATA.Database[guildId].GetHbitmap(); BitmapSource bs; try { bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } finally { Gdi32.DeleteObject(ip); } return(bs); }
/// <summary> /// Computes the region for a regular or partially transparent background that is bounded by a specified /// rectangle. Return null if the region cannot be created. /// [See win32 equivalent.] /// </summary> public Region?GetBackgroundRegion(IDeviceContext dc, Rectangle bounds) { if (dc is null) { throw new ArgumentNullException(nameof(dc)); } if (bounds.Width < 0 || bounds.Height < 0) { return(null); } using var hdc = new DeviceContextHdcScope(dc); RECT boundsRect = bounds; _lastHResult = GetThemeBackgroundRegion(this, hdc, Part, State, ref boundsRect, out Gdi32.HRGN hRegion); // GetThemeBackgroundRegion returns a null hRegion if it fails to create one, it could be because the bounding // box is too big. For more info see code in %xpsrc%\shell\themes\uxtheme\imagefile.cpp if you have an enlistment to it. if (hRegion.IsNull) { return(null); } // From the GDI+ sources it doesn't appear as if they take ownership of the hRegion, so this is safe to do. // We need to DeleteObject in order to not leak. Region region = Region.FromHrgn(hRegion.Handle); Gdi32.DeleteObject(hRegion); return(region); }
internal void Dispose(bool disposing) { bool deletedHandle = false; if (_ownHandle) { if (!_ownedByCacheManager || !disposing) { // If we were ever owned by the CacheManger and we're being disposed // we can be sure that we're not in use by any DC's (otherwise Dispose() wouldn't have been called) // skip the check IsFontInUse check in this case. // Also skip the check if disposing == false, because the cache is thread-static // and that means we're being called from the finalizer. if (_everOwnedByCacheManager || !disposing || !DeviceContexts.IsFontInUse(this)) { Debug.Assert(Hfont != IntPtr.Zero, "Unexpected null hFont."); DbgUtil.AssertFinalization(this, disposing); Gdi32.DeleteObject(Hfont); Hfont = IntPtr.Zero; _ownHandle = false; deletedHandle = true; } } } if (disposing && (deletedHandle || !_ownHandle)) { GC.SuppressFinalize(this); } }
// 特定窗口的截图对象 private Image CaptureWindow(IntPtr handle) { // 获得目标窗口的hDC SafeDCHandle hdcSrc = User32.GetWindowDC(handle); var screenSize = GetScreenPhysicalSzie(); // create a device context we can copy to var hdcDest = Gdi32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, screenSize.Width, screenSize.Height); // select the bitmap object IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap); // bitblt over Gdi32.BitBlt(hdcDest.HWnd, 0, 0, screenSize.Width, screenSize.Height, hdcSrc.HWnd, 0, 0, WindowsAPIUtils.SRCCOPY); // restore selection Gdi32.SelectObject(hdcDest, hOld); // clean up Gdi32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc.HWnd); // get a .NET image object for it Image img = Image.FromHbitmap(hBitmap); // free up the Bitmap object Gdi32.DeleteObject(hBitmap); return(img); }
private void ExcludeRegion() { if (!ShouldExlcudeRegion) { return; } IntPtr hRegion = IntPtr.Zero; try { hRegion = GetRegion(); if (hRegion != IntPtr.Zero) { Gdi32.SetWindowRgn(Handle, hRegion, false); } if (Region != IntPtr.Zero) { Gdi32.DeleteObject(Region); } Region = hRegion; } finally { if (hRegion != IntPtr.Zero) { Gdi32.DeleteObject(hRegion); } } }
/// <inheritdoc /> public override void Draw(Graphics g) { CURSORINFO cursorInfo; cursorInfo.cbSize = Marshal.SizeOf(typeof(CURSORINFO)); if (User32.GetCursorInfo(out cursorInfo)) { if (cursorInfo.flags == CursorState.CURSOR_SHOWING) { // We need to get the icon to get the "Hotspot" (aka offset) var hicon = User32.CopyIcon(cursorInfo.hCursor); if (hicon != IntPtr.Zero) { if (User32.GetIconInfo(hicon, out var iconInfo)) { // Calculate the positions, relative to the bounds of the image relative to the desktop. var x = cursorInfo.ptScreenPos.X - DesktopBounds.Left; var y = cursorInfo.ptScreenPos.Y - DesktopBounds.Top; User32.DrawIconEx(g.GetHdc(), x - iconInfo.xHotspot, y - iconInfo.yHotspot, cursorInfo.hCursor, 0, 0, 0, IntPtr.Zero, 0x0003); g.ReleaseHdc(); } Gdi32.DeleteObject(iconInfo.hbmColor); Gdi32.DeleteObject(iconInfo.hbmMask); } User32.DestroyIcon(hicon); } Gdi32.DeleteObject(cursorInfo.hCursor); } }
private Image CaptureWindow(IntPtr handle) { // get te hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // get the size RECT windowRect = new RECT(); User32.GetWindowRect(handle, out windowRect); int width = windowRect.Right - windowRect.Left; int height = windowRect.Bottom - windowRect.Top; // create a device context we can copy to IntPtr hdcDest = Gdi32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap); // bitblt over Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Gdi32.SRCCOPY); // restore selection Gdi32.SelectObject(hdcDest, hOld); // clean up Gdi32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); // get a .NET image object for it Image img = Image.FromHbitmap(hBitmap); // free up the Bitmap object Gdi32.DeleteObject(hBitmap); return(img); }
public static Image CaptureWindow(IntPtr handle, double scale) { var rectangle = Windows.GetWindowRect(handle); var posX = (int)((rectangle.X + Util.Constants.LeftOffset) * scale); var posY = (int)((rectangle.Y + Util.Constants.TopOffset) * scale); var width = (int)((rectangle.Width - Util.Constants.HorizontalOffset) * scale); var height = (int)((rectangle.Height - Util.Constants.VerticalOffset) * scale); var hDesk = User32.GetDesktopWindow(); var hSrce = User32.GetWindowDC(hDesk); var hDest = Gdi32.CreateCompatibleDC(hSrce); var hBmp = Gdi32.CreateCompatibleBitmap(hSrce, width, height); var hOldBmp = Gdi32.SelectObject(hDest, hBmp); var b = Gdi32.BitBlt(hDest, 0, 0, width, height, hSrce, posX, posY, CopyPixelOperations.SourceCopy | CopyPixelOperations.CaptureBlt); try { return(Image.FromHbitmap(hBmp)); } catch (Exception ex) { LogWriter.Log(ex, "Impossible to get screenshot of the screen"); } finally { Gdi32.SelectObject(hDest, hOldBmp); Gdi32.DeleteObject(hBmp); Gdi32.DeleteDC(hDest); User32.ReleaseDC(hDesk, hSrce); } return(null); }
public void SetBitmap(Bitmap bitmap, byte opacity) { hBitmap = IntPtr.Zero; oldBitmap = IntPtr.Zero; topPos = Location; bmpbase = bitmap.Clone() as Bitmap; Bounds = new Rectangle(Location, bmpbase.Size); Size size = bmpbase.Size; wDC = User32.GetDC(this.Handle); // was IntPtr.Zero memDC = Gdi32.CreateCompatibleDC(wDC); try { hBitmap = bmpbase.GetHbitmap(Color.FromArgb(0)); // Color.FromArgb(0);grab a GDI handle from this GDI+ bitmap oldBitmap = Gdi32.SelectObject(memDC, hBitmap); Point pointSource = new Point(0, 0); BlendUtil.LayeredAlphaBlend(opacity, Handle, wDC, memDC, ref topPos, ref size, ref pointSource); OnMove(null); } finally { User32.ReleaseDC(IntPtr.Zero, wDC); if (hBitmap != IntPtr.Zero) { Gdi32.SelectObject(memDC, oldBitmap); //Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win GDI and it's working fine without any resource leak. Gdi32.DeleteObject(hBitmap); } Gdi32.DeleteDC(memDC); } }
/// <summary> /// Captures the screen using the SourceCopy | CaptureBlt. /// </summary> /// <param name="height">Height of the capture region.</param> /// <param name="positionX">Source capture Left position.</param> /// <param name="positionY">Source capture Top position.</param> /// <param name="width">Width of the capture region.</param> /// <returns>A bitmap with the capture rectangle.</returns> public static Image CaptureScreenAsBitmap(int width, int height, int positionX, int positionY) { var hDesk = User32.GetDesktopWindow(); var hSrce = User32.GetWindowDC(hDesk); var hDest = Gdi32.CreateCompatibleDC(hSrce); var hBmp = Gdi32.CreateCompatibleBitmap(hSrce, width, height); var hOldBmp = Gdi32.SelectObject(hDest, hBmp); try { var b = Gdi32.BitBlt(hDest, 0, 0, width, height, hSrce, positionX, positionY, CopyPixelOperations.SourceCopy | CopyPixelOperations.CaptureBlt); return(b ? Image.FromHbitmap(hBmp) : null); } catch (Exception) { //LogWriter.Log(ex, "Impossible to get screenshot of the screen"); } finally { Gdi32.SelectObject(hDest, hOldBmp); Gdi32.DeleteObject(hBmp); Gdi32.DeleteDC(hDest); User32.ReleaseDC(hDesk, hSrce); } return(null); }
/// <summary> /// Captures the screen using the SourceCopy | CaptureBlt. /// </summary> /// <param name="width">The size of the final image.</param> /// <param name="height">The size of the final image.</param> /// <param name="positionX">Source capture Left position.</param> /// <param name="positionY">Source capture Top position.</param> /// <returns>A bitmap with the capture rectangle.</returns> public static BitmapSource CaptureScreenAsBitmapSource(int width, int height, int positionX, int positionY) { var hDesk = User32.GetDesktopWindow(); var hSrce = User32.GetWindowDC(hDesk); var hDest = Gdi32.CreateCompatibleDC(hSrce); var hBmp = Gdi32.CreateCompatibleBitmap(hSrce, width, height); var hOldBmp = Gdi32.SelectObject(hDest, hBmp); try { var b = Gdi32.BitBlt(hDest, 0, 0, width, height, hSrce, positionX, positionY, CopyPixelOperations.SourceCopy | CopyPixelOperations.CaptureBlt); //return Image.FromHbitmap(hBmp); return(System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBmp, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())); } catch (Exception ex) { LogWriter.Log(ex, "Impossible to get screenshot of the screen"); } finally { Gdi32.SelectObject(hDest, hOldBmp); Gdi32.DeleteObject(hBmp); Gdi32.DeleteDC(hDest); User32.ReleaseDC(hDesk, hSrce); } return(null); }
private void DisposeGDIObject(ref IntPtr hObject) { IntPtr tmp = hObject; hObject = IntPtr.Zero; Gdi32.DeleteObject(tmp); }
private void FromWindow(IntPtr hwnd) { IntPtr hDc = User32.GetDC(hwnd); IntPtr memhDC = Gdi32.CreateCompatibleDC(hDc); User32.RECT clientSize; User32.GetClientRect(hwnd, out clientSize); Point bounds = new Point(clientSize.Right - clientSize.Left, clientSize.Bottom - clientSize.Top); IntPtr hBmp = Gdi32.CreateCompatibleBitmap(hDc, bounds.X, bounds.Y); Gdi32.SelectObject(memhDC, hBmp); Gdi32.BitBlt(memhDC, 0, 0, bounds.X, bounds.Y, hDc, 0, 0, Gdi32.TernaryRasterOperations.SRCCOPY); Bitmap bmp = Bitmap.FromHbitmap(hBmp); Gdi32.DeleteObject(hBmp); User32.ReleaseDC(hwnd, hDc); Gdi32.DeleteDC(memhDC); FromBitmap(bmp); }
public Bitmap CaptureWindow(IntPtr handle) { IntPtr hdcSrc = User32.GetWindowDC(handle); User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; IntPtr hdcDest = Gdi32.CreateCompatibleDC(hdcSrc); IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height); IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap); Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Gdi32.SRCCOPY); Gdi32.SelectObject(hdcDest, hOld); Gdi32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); Image image = Image.FromHbitmap(hBitmap); Gdi32.DeleteObject(hBitmap); return(new Bitmap(image)); }
protected void UpdateLayeredWindow(Point point, Size size, byte alpha, bool wrap) { // Create bitmap for drawing onto Bitmap memoryBitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(memoryBitmap)) { Rectangle area = new Rectangle(0, 0, size.Width, size.Height); // Draw the background area Draw(g, area, wrap); // Get hold of the screen DC IntPtr hDC = User32.GetDC(IntPtr.Zero); // Create a memory based DC compatible with the screen DC IntPtr memoryDC = Gdi32.CreateCompatibleDC(hDC); // Get access to the bitmap handle contained in the Bitmap object IntPtr hBitmap = memoryBitmap.GetHbitmap(Color.FromArgb(0)); // Select this bitmap for updating the window presentation IntPtr oldBitmap = Gdi32.SelectObject(memoryDC, hBitmap); // New window size SIZE ulwsize; ulwsize.cx = size.Width; ulwsize.cy = size.Height; // New window position POINT topPos; topPos.x = point.X; topPos.y = point.Y; // Offset into memory bitmap is always zero POINT pointSource; pointSource.x = 0; pointSource.y = 0; // We want to make the entire bitmap opaque BLENDFUNCTION blend = new BLENDFUNCTION(); blend.BlendOp = (byte)AlphaFlags.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = alpha; blend.AlphaFormat = (byte)AlphaFlags.AC_SRC_ALPHA; // Tell operating system to use our bitmap for painting User32.UpdateLayeredWindow(Handle, hDC, ref topPos, ref ulwsize, memoryDC, ref pointSource, 0, ref blend, (int)UpdateLayeredWindowsFlags.ULW_ALPHA); // Put back the old bitmap handle Gdi32.SelectObject(memoryDC, oldBitmap); // Cleanup resources User32.ReleaseDC(IntPtr.Zero, hDC); Gdi32.DeleteObject(hBitmap); Gdi32.DeleteDC(memoryDC); } }
public void DiscardDeviceResources() { _renderTarget?.Dispose(); _renderTarget = null; if (_memDC != IntPtr.Zero && _hBitmap != IntPtr.Zero && _hOldBitmap != IntPtr.Zero) { Gdi32.SelectObject(_memDC, _hOldBitmap); Gdi32.DeleteObject(_hBitmap); } if (_memDC != IntPtr.Zero) { Gdi32.DeleteDC(_memDC); } if (_screenDC != IntPtr.Zero) { User32.ReleaseDC(IntPtr.Zero, _screenDC); } _hOldBitmap = IntPtr.Zero; _hBitmap = IntPtr.Zero; _memDC = IntPtr.Zero; _screenDC = IntPtr.Zero; GC.Collect(); }
private void SetSpotInfo() { int x = _originX; int y = _originY; IntPtr hwnd = User32.GetDesktopWindow(); IntPtr dc = User32.GetWindowDC(hwnd); IntPtr memDC = Gdi32.CreateCompatibleDC(dc); IntPtr hbm = Gdi32.CreateCompatibleBitmap(dc, _spotWidth, _spotHeight); IntPtr oldbm = Gdi32.SelectObject(memDC, hbm); Gdi32.BitBlt( memDC, 0, 0, this.SpotWidth, this.SpotHeight, dc, x, y, 0x40CC0020 ); _currentSpotBmp = Image.FromHbitmap(hbm); Gdi32.SelectObject(memDC, oldbm); Gdi32.DeleteObject(hbm); Gdi32.DeleteDC(memDC); User32.ReleaseDC(hwnd, dc); }
/// <summary> /// Windows only window capture by handle /// </summary> /// <param name="handle"></param> /// <returns></returns> public static byte[] WindowsCapturePng(IntPtr handle) { // get te hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // get the size User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; // create a device context we can copy to IntPtr hdcDest = Gdi32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap); // bitblt over Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Gdi32.SRCCOPY); // restore selection Gdi32.SelectObject(hdcDest, hOld); // clean up Gdi32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); byte[] ret = HbitmapToPng(hBitmap); // free up the Bitmap object Gdi32.DeleteObject(hBitmap); return(ret); }
private IntPtr GetRegion() { IntPtr hShadowReg = IntPtr.Zero; IntPtr hOwnerReg = IntPtr.Zero; IntPtr hRegion = IntPtr.Zero; try { var rect = new RECT(); User32.GetWindowRect(_handle, ref rect); hShadowReg = Gdi32.CreateRectRgn(0, 0, rect.Width, rect.Height); hOwnerReg = Gdi32.CreateRectRgn(GetRegionRect()); hRegion = CombineRgn(hShadowReg, hOwnerReg, 4); } finally { if (hShadowReg != IntPtr.Zero) { Gdi32.DeleteObject(hShadowReg); } if (hOwnerReg != IntPtr.Zero) { Gdi32.DeleteObject(hOwnerReg); } } return(hRegion); }
public static void DrawLine(Graphics g, Pen pen, int x1, int y1, int x2, int y2) { if (g == null) { throw new ArgumentNullException("g"); } if (pen == null) { throw new ArgumentNullException("pen"); } IntPtr hDC = g.GetHdc(); IntPtr hPen = Gdi32.CreatePen(pen); IntPtr oldBrush = Gdi32.SelectObject(hDC, Gdi32.GetStockObject(WinGdi.NULL_BRUSH)); IntPtr oldPen = Gdi32.SelectObject(hDC, hPen); Gdi32.MoveTo(hDC, x1, y1); Gdi32.LineTo(hDC, x2, y2); Gdi32.SelectObject(hDC, oldBrush); Gdi32.SelectObject(hDC, oldPen); Gdi32.DeleteObject(hPen); g.ReleaseHdc(hDC); }
// this DC is cached and should only be deleted on Dispose or when the size changes. public Gdi32.HDC GetCachedItemDC(Gdi32.HDC toolStripHDC, Size bitmapSize) { if (_cachedHDCSize.Width < bitmapSize.Width || _cachedHDCSize.Height < bitmapSize.Height) { if (_cachedItemHDC.IsNull) { // Create a new DC - we don't have one yet. _cachedItemHDC = Gdi32.CreateCompatibleDC(toolStripHDC); } // Create compatible bitmap with the correct size. _cachedItemBitmap = Gdi32.CreateCompatibleBitmap(toolStripHDC, bitmapSize.Width, bitmapSize.Height); Gdi32.HGDIOBJ oldBitmap = Gdi32.SelectObject(_cachedItemHDC, _cachedItemBitmap); // Delete the old bitmap if (!oldBitmap.IsNull) { Gdi32.DeleteObject(oldBitmap); } // remember what size we created. _cachedHDCSize = bitmapSize; } return(_cachedItemHDC); }
public Region GetBackgroundRegion(IDeviceContext dc, Rectangle bounds) { if (dc == null) { throw new ArgumentNullException(nameof(dc)); } if (bounds.Width < 0 || bounds.Height < 0) { return(null); } IntPtr hRegion = IntPtr.Zero; using (WindowsGraphicsWrapper wgr = new WindowsGraphicsWrapper(dc, AllGraphicsProperties)) { HandleRef hdc = new HandleRef(wgr, wgr.WindowsGraphics.DeviceContext.Hdc); lastHResult = SafeNativeMethods.GetThemeBackgroundRegion(new HandleRef(this, Handle), hdc, part, state, new NativeMethods.COMRECT(bounds), ref hRegion); } // GetThemeBackgroundRegion returns a null hRegion if it fails to create one, it could be because the bounding // box is too big. For more info see code in %xpsrc%\shell\themes\uxtheme\imagefile.cpp if you have an enlistment to it. if (hRegion == IntPtr.Zero) { return(null); } // From the GDI+ sources it doesn't appear as if they take ownership of the hRegion, so this is safe to do. // We need to DeleteObject in order to not leak. Region region = Region.FromHrgn(hRegion); Gdi32.DeleteObject(hRegion); return(region); }
static Bitmap CaptureRegionUnmanaged(Rectangle Region, bool IncludeCursor = false) { IntPtr hSrc = Gdi32.CreateDC("DISPLAY", null, null, 0), hDest = Gdi32.CreateCompatibleDC(hSrc), hBmp = Gdi32.CreateCompatibleBitmap(hSrc, Region.Width, Region.Height), hOldBmp = Gdi32.SelectObject(hDest, hBmp); Gdi32.BitBlt(hDest, 0, 0, Region.Width, Region.Height, hSrc, Region.Left, Region.Top, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); var bmp = Image.FromHbitmap(hBmp); Gdi32.SelectObject(hDest, hOldBmp); Gdi32.DeleteObject(hBmp); Gdi32.DeleteDC(hDest); Gdi32.DeleteDC(hSrc); var clone = bmp.Clone(new Rectangle(Point.Empty, bmp.Size), PixelFormat.Format24bppRgb); if (IncludeCursor) { new MouseCursor().Draw(clone, Region.Location); } return(clone); }
private unsafe void CreateDitherBrush() { Debug.Assert(_hbrushDither.IsNull, "Brush should not be recreated."); short *patternBits = stackalloc short[] { unchecked ((short)0xAAAA), unchecked ((short)0x5555), unchecked ((short)0xAAAA), unchecked ((short)0x5555), unchecked ((short)0xAAAA), unchecked ((short)0x5555), unchecked ((short)0xAAAA), unchecked ((short)0x5555) }; Gdi32.HBITMAP hbitmapTemp = Gdi32.CreateBitmap(8, 8, 1, 1, patternBits); Debug.Assert( !hbitmapTemp.IsNull, "could not create dither bitmap. Page selector UI will not be correct"); if (!hbitmapTemp.IsNull) { _hbrushDither = Gdi32.CreatePatternBrush(hbitmapTemp); Debug.Assert( !_hbrushDither.IsNull, "Unable to created dithered brush. Page selector UI will not be correct"); Gdi32.DeleteObject(hbitmapTemp); } }
public Bitmap CaptureWindowGDI(IntPtr handle) { var hdcSrc = User32.GetWindowDC(handle); var windowRect = new RECT(); User32.GetWindowRect(handle, ref windowRect); var width = windowRect.right - windowRect.left; var height = windowRect.bottom - windowRect.top; var hdcDest = Gdi32.CreateCompatibleDC(hdcSrc); var hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height); var hOld = Gdi32.SelectObject(hdcDest, hBitmap); Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, 13369376); Gdi32.SelectObject(hdcDest, hOld); Gdi32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); var image = Image.FromHbitmap(hBitmap); Gdi32.DeleteObject(hBitmap); return(image); }
public Bitmap Capture() { if (GetWindowThreadProcessId() == 0) { return(new Bitmap(1, 1, PixelFormat.Format24bppRgb)); } var deviceSrc = User32.GetWindowDC(this.windowHandle); User32.GetWindowRect(this.windowHandle, ref this.rect); var deviceDest = Gdi32.CreateCompatibleDC(deviceSrc); var bitmapHandle = Gdi32.CreateCompatibleBitmap( deviceSrc, this.rect.Width, this.rect.Height); var oldHandle = Gdi32.SelectObject(deviceDest, bitmapHandle); Gdi32.BitBlt(deviceDest, 0, 0, this.rect.Width, this.rect.Height, deviceSrc, 0, 0, 0x00CC0020); Gdi32.SelectObject(deviceDest, oldHandle); Gdi32.DeleteDC(deviceDest); User32.ReleaseDC(this.windowHandle, deviceSrc); var img = Image.FromHbitmap(bitmapHandle); Gdi32.DeleteObject(bitmapHandle); return(img); }
void IDisposable.Dispose() { if (HDC == IntPtr.Zero || _hMetafileDC == IntPtr.Zero || _hBitmap == IntPtr.Zero) { return; } bool success; try { success = DICopy(_hMetafileDC, HDC, _destRect, true); Debug.Assert(success, "DICopy() failed."); Gdi32.SelectObject(HDC, _hOriginalBmp); success = Gdi32.DeleteObject(_hBitmap).IsTrue(); Debug.Assert(success, "DeleteObject() failed."); success = Gdi32.DeleteDC(HDC); Debug.Assert(success, "DeleteObject() failed."); } finally { // Dispose is done. Set all the handles to IntPtr.Zero so this way the Dispose method executes only once. HDC = IntPtr.Zero; _hBitmap = IntPtr.Zero; _hOriginalBmp = IntPtr.Zero; GC.SuppressFinalize(this); } }
/* * DrawGridGdi */ /// <summary> /// Draw the grid overlay using native GDI. /// </summary> /// <param name="dc">Specifies drawing context for this <see cref="T:Genetibase.UI.NuGenPushGraphBar"/>.</param> /// <param name="rect">Specifies the <see cref="T:System.Drawing.Rectangle"/> to draw within.</param> /// <param name="gridColor">Specifies the grid color.</param> /// <param name="gridStep">Specifies the grid step.</param> protected virtual void DrawGridGdi(IntPtr dc, Rectangle rect, Color gridColor, int gridStep) { using (Pen pen = new Pen(gridColor)) { IntPtr hPen = Gdi32.CreatePen(pen); IntPtr hOldPen = Gdi32.SelectObject(dc, hPen); for (int row = rect.Height; row >= 0; row -= gridStep) { Gdi32.MoveTo(dc, rect.Left, row); Gdi32.LineTo(dc, rect.Right, row); } for (int col = rect.Left + this.pushOffset; col < rect.Right; col += gridStep) { if (col < rect.Left) { continue; } Gdi32.MoveTo(dc, col, 0); Gdi32.LineTo(dc, col, rect.Height); } Gdi32.SelectObject(dc, hOldPen); Gdi32.DeleteObject(hPen); } }