/// <include file='doc\DebugHandleTracker.uex' path='docs/doc[@for="DebugHandleTracker.HandleType.HandleEntry.ToString"]/*' /> /// <devdoc> /// Converts this handle to a printable string. the string consists /// of the handle value along with the callstack for it's /// allocation. /// </devdoc> public string ToString(HandleType type) { StackParser sp = new StackParser(callStack); // Discard all of the stack up to and including the "Handle.create" call // sp.DiscardTo("HandleCollector.Add"); // Skip the next call as it is always a debug wrapper // sp.DiscardNext(); // Now recreate the leak list with ten stack entries maximum, and // put it all on one line. // sp.Truncate(10); string description = ""; if (type.name.Equals("GDI") || type.name.Equals("HDC")) { int objectType = UnsafeNativeMethods.GetObjectType(new HandleRef(null, handle)); switch (objectType) { case NativeMethods.OBJ_DC: description = "normal DC"; break; case NativeMethods.OBJ_MEMDC: description = "memory DC"; break; case NativeMethods.OBJ_METADC: description = "metafile DC"; break; case NativeMethods.OBJ_ENHMETADC: description = "enhanced metafile DC"; break; case NativeMethods.OBJ_PEN: description = "Pen"; break; case NativeMethods.OBJ_BRUSH: description = "Brush"; break; case NativeMethods.OBJ_PAL: description = "Palette"; break; case NativeMethods.OBJ_FONT: description = "Font"; break; case NativeMethods.OBJ_BITMAP: description = "Bitmap"; break; case NativeMethods.OBJ_REGION: description = "Region"; break; case NativeMethods.OBJ_METAFILE: description = "Metafile"; break; case NativeMethods.OBJ_EXTPEN: description = "Extpen"; break; default: description = "?"; break; } description = " (" + description + ")"; } return(Convert.ToString((int)handle, 16) + description + ": " + sp.ToString()); }
internal MetafileDCWrapper(HandleRef hOriginalDC, Size size) { Debug.Assert(UnsafeNativeMethods.GetObjectType(hOriginalDC) == NativeMethods.OBJ_ENHMETADC, "Why wrap a non-Enhanced MetaFile DC?"); // Security fix: make sure the size has non-negative width and height. if (size.Width < 0 || size.Height < 0) { throw new ArgumentException(nameof(size), SR.ControlMetaFileDCWrapperSizeInvalid); } _hMetafileDC = hOriginalDC; _destRect = new Interop.RECT(0, 0, size.Width, size.Height); HDC = Interop.Gdi32.CreateCompatibleDC(IntPtr.Zero); int planes = Interop.Gdi32.GetDeviceCaps(HDC, Interop.Gdi32.DeviceCapability.PLANES); int bitsPixel = Interop.Gdi32.GetDeviceCaps(HDC, Interop.Gdi32.DeviceCapability.BITSPIXEL); _hBitmap = SafeNativeMethods.CreateBitmap(size.Width, size.Height, planes, bitsPixel, IntPtr.Zero); _hOriginalBmp = Interop.Gdi32.SelectObject(HDC, _hBitmap); }
// CreateCompatibleDIB // // Create a DIB section with an optimal format w.r.t. the specified hdc. // // If DIB <= 8bpp, then the DIB color table is initialized based on the // specified palette. If the palette handle is NULL, then the system // palette is used. // // Note: The hdc must be a direct DC (not an info or memory DC). // // Note: On palettized displays, if the system palette changes the // UpdateDIBColorTable function should be called to maintain // the identity palette mapping between the DIB and the display. // // Returns: // Valid bitmap handle if successful, NULL if error. // // History: // 23-Jan-1996 -by- Gilman Wong [gilmanw] // Wrote it. // // 15-Nov-2000 -by- Chris Anderson [chrisan] // Ported it to C#. // private IntPtr CreateCompatibleDIB(IntPtr hdc, IntPtr hpal, int ulWidth, int ulHeight, ref IntPtr ppvBits) { if (hdc == IntPtr.Zero) { throw new ArgumentNullException("hdc"); } IntPtr hbmRet = IntPtr.Zero; NativeMethods.BITMAPINFO_FLAT pbmi = new NativeMethods.BITMAPINFO_FLAT(); // // Validate hdc. // if (UnsafeNativeMethods.GetObjectType(new HandleRef(null, hdc)) != NativeMethods.OBJ_DC) { throw new ArgumentException("hdc"); } if (bFillBitmapInfo(hdc, hpal, ref pbmi)) { // // Change bitmap size to match specified dimensions. // pbmi.bmiHeader_biWidth = ulWidth; pbmi.bmiHeader_biHeight = ulHeight; if (pbmi.bmiHeader_biCompression == NativeMethods.BI_RGB) { pbmi.bmiHeader_biSizeImage = 0; } else { if (pbmi.bmiHeader_biBitCount == 16) { pbmi.bmiHeader_biSizeImage = ulWidth * ulHeight * 2; } else if (pbmi.bmiHeader_biBitCount == 32) { pbmi.bmiHeader_biSizeImage = ulWidth * ulHeight * 4; } else { pbmi.bmiHeader_biSizeImage = 0; } } pbmi.bmiHeader_biClrUsed = 0; pbmi.bmiHeader_biClrImportant = 0; // // Create the DIB section. Let Win32 allocate the memory and return // a pointer to the bitmap surface. // hbmRet = SafeNativeMethods.CreateDIBSection(new HandleRef(null, hdc), ref pbmi, NativeMethods.DIB_RGB_COLORS, ref ppvBits, IntPtr.Zero, 0); #if DEBUG if (DoubleBuffering.TraceVerbose) { DumpBitmapInfo(ref pbmi); } #endif if (hbmRet == IntPtr.Zero) { #if DEBUG DumpBitmapInfo(ref pbmi); #endif throw new Win32Exception(Marshal.GetLastWin32Error()); } } return(hbmRet); }