Example #1
0
        /// <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));
            }
        }
        /// <summary>
        ///  Internal version of constructor for performance. We try to avoid getting the graphics object until needed.
        /// </summary>
        public DrawingEventArgs(
            Gdi32.HDC dc,
            Rectangle clipRect,
            DrawingEventFlags flags)
        {
            ArgumentValidation.ThrowIfNull(dc);

#if DEBUG
            Gdi32.OBJ type = Gdi32.GetObjectType(dc);
            Debug.Assert(type == Gdi32.OBJ.DC ||
                         type == Gdi32.OBJ.ENHMETADC ||
                         type == Gdi32.OBJ.MEMDC ||
                         type == Gdi32.OBJ.METADC);
#endif

            _hdc          = dc;
            _graphics     = null;
            _oldPalette   = default;
            Flags         = flags;
            ClipRectangle = clipRect;
        }
Example #3
0
        /// <summary>
        ///  Internal version of constructor for performance. We try to avoid getting the graphics object until needed.
        /// </summary>
        public DrawingEventArgs(
            Gdi32.HDC dc,
            Rectangle clipRect,
            DrawingEventFlags flags)
        {
            if (dc.IsNull)
            {
                throw new ArgumentNullException(nameof(dc));
            }

#if DEBUG
            Gdi32.OBJ type = Gdi32.GetObjectType(dc);
            Debug.Assert(type == Gdi32.OBJ.DC ||
                         type == Gdi32.OBJ.ENHMETADC ||
                         type == Gdi32.OBJ.MEMDC ||
                         type == Gdi32.OBJ.METADC);
#endif

            _hdc          = dc;
            _graphics     = null;
            _oldPalette   = default;
            Flags         = flags;
            ClipRectangle = clipRect;
        }