private Gdi32.HBITMAP GetCompatibleBitmap(Bitmap bm) { using var screenDC = User32.GetDcScope.ScreenDC; // GDI+ returns a DIBSECTION based HBITMAP. The clipboard deals well // only with bitmaps created using CreateCompatibleBitmap(). So, we // convert the DIBSECTION into a compatible bitmap. Gdi32.HBITMAP hBitmap = bm.GetHBITMAP(); // Create a compatible DC to render the source bitmap. using var sourceDC = new Gdi32.CreateDcScope(screenDC); using var sourceBitmapSelection = new Gdi32.SelectObjectScope(sourceDC, hBitmap); // Create a compatible DC and a new compatible bitmap. using var destinationDC = new Gdi32.CreateDcScope(screenDC); Gdi32.HBITMAP bitmap = Gdi32.CreateCompatibleBitmap(screenDC, bm.Size.Width, bm.Size.Height); // Select the new bitmap into a compatible DC and render the blt the original bitmap. using var destinationBitmapSelection = new Gdi32.SelectObjectScope(destinationDC, bitmap); Gdi32.BitBlt( destinationDC, 0, 0, bm.Size.Width, bm.Size.Height, sourceDC, 0, 0, Gdi32.ROP.SRCCOPY); return(bitmap); }
// 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); }
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 static int Add(IHandle himl, Gdi32.HBITMAP hbmImage, Gdi32.HBITMAP hbmMask) { int result = Add(himl.Handle, hbmImage, hbmMask); GC.KeepAlive(himl); return(result); }
public void Dispose() { if (!hbmMask.IsNull) { Gdi32.DeleteObject(hbmMask); hbmMask = default; } if (!hbmColor.IsNull) { Gdi32.DeleteObject(hbmColor); hbmColor = default; } }
public void Dispose() { if (!_cachedItemHDC.IsNull) { if (!_cachedItemBitmap.IsNull) { Gdi32.DeleteObject(_cachedItemBitmap); } // delete the DC itself. Gdi32.DeleteDC(_cachedItemHDC); } _cachedItemHDC = default; _cachedItemBitmap = default; _cachedHDCSize = Size.Empty; GC.SuppressFinalize(this); }
public static extern int Add(IntPtr himl, Gdi32.HBITMAP hbmImage, Gdi32.HBITMAP hbmMask);
public static partial int Add(IntPtr himl, Gdi32.HBITMAP hbmImage, Gdi32.HBITMAP hbmMask);