/// <summary> /// Gets an <see cref="Graphics"/> based off of the primary display. /// </summary> /// <remarks> /// Use in a using statement for proper cleanup. /// /// When disposed the <see cref="Graphics"/> object will be disposed and the underlying <see cref="Gdi32.HDC"/> /// will be returned to the cache. Do NOT change the state of the underlying DC (clipping, selecting objects /// into it) without restoring the state. If you must pass the scope to another method it must be passed by /// reference or you risk double disposal and accidentally returning extra copies to the cache. /// </remarks> public static ScreenGraphicsScope GetScreenDCGraphics() { ScreenDcCache.ScreenDcScope scope = GetScreenHdc(); try { return(new ScreenGraphicsScope(ref scope)); } catch (OutOfMemoryException) { // GDI+ throws OOM if it can't confirm a valid HDC. We'll throw a more meaningful error here // for easier diagnosis. if (scope.HDC.IsNull) { throw new ArgumentNullException("hdc"); } Gdi32.OBJ type = Gdi32.GetObjectType(scope.HDC); if (type == Gdi32.OBJ.DC || type == Gdi32.OBJ.ENHMETADC || type == Gdi32.OBJ.MEMDC || type == Gdi32.OBJ.METADC) { // Not sure what is wrong in this case, throw the original. throw; } throw new InvalidOperationException(string.Format(SR.InvalidHdcType, type)); } }
public ScreenGraphicsScope(ref ScreenDcCache.ScreenDcScope scope) { _dcScope = scope; Graphics = scope.HDC.CreateGraphics(); }
/// <summary> /// Gets an <see cref="Graphics"/> based off of the primary display. /// </summary> /// <remarks> /// Use in a using statement for proper cleanup. /// /// When disposed the <see cref="Graphics"/> object will be disposed and the underlying <see cref="Gdi32.HDC"/> /// will be returned to the cache. Do NOT change the state of the underlying DC (clipping, selecting objects /// into it) without restoring the state. If you must pass the scope to another method it must be passed by /// reference or you risk double disposal and accidentally returning extra copies to the cache. /// </remarks> public static ScreenGraphicsScope GetScreenDCGraphics() { ScreenDcCache.ScreenDcScope scope = GetScreenHdc(); return(new ScreenGraphicsScope(ref scope)); }