/// <summary>
        ///  For internal use to improve performance. DO NOT use this method if you modify the Graphics Clip or Transform.
        /// </summary>
        internal Graphics GetOrCreateGraphicsInternal(Action <Graphics>?creationAction = null)
        {
            if (_graphics is null)
            {
                Debug.Assert(!_hdc.IsNull);

                // We need to manually unset the palette here so this scope shouldn't be disposed
                var paletteScope = Gdi32.SelectPaletteScope.HalftonePalette(
                    _hdc,
                    forceBackground: false,
                    realizePalette: false);

                GC.SuppressFinalize(paletteScope);

                _oldPalette = paletteScope.HPalette;

                _graphics          = Graphics.FromHdcInternal((IntPtr)_hdc);
                _graphics.PageUnit = GraphicsUnit.Pixel;
                creationAction?.Invoke(_graphics);

                CheckGraphicsForState(_graphics, Flags);
            }

            return(_graphics);
        }
Beispiel #2
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)
 {
     _hdc          = dc;
     _graphics     = null;
     _oldPalette   = default;
     Flags         = flags;
     ClipRectangle = clipRect;
 }
 public DrawingEventArgs(
     Graphics graphics,
     Rectangle clipRect,
     DrawingEventFlags flags)
 {
     _graphics   = graphics.OrThrowIfNull();
     _hdc        = default;
     _oldPalette = default;
     CheckGraphicsForState(graphics, flags);
     ClipRectangle = clipRect;
     Flags         = flags;
 }
Beispiel #4
0
 public DrawingEventArgs(
     Graphics graphics,
     Rectangle clipRect,
     DrawingEventFlags flags)
 {
     _graphics   = graphics ?? throw new ArgumentNullException(nameof(graphics));
     _hdc        = default;
     _oldPalette = default;
     CheckGraphicsForState(graphics, flags);
     ClipRectangle = clipRect;
     Flags         = flags;
 }
        internal void Dispose(bool disposing)
        {
            if (disposing)
            {
                // Only dispose the graphics object if we created it via the HDC.
                if (_graphics is not null && !_hdc.IsNull)
                {
                    _graphics.Dispose();
                }
            }

            if (!_oldPalette.IsNull && !_hdc.IsNull)
            {
                Gdi32.SelectPalette(_hdc, _oldPalette, BOOL.FALSE);
                _oldPalette = default;
            }
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // Only dispose the graphics object if we created it via the dc.
                if (_graphics != null && !_dc.IsNull)
                {
                    _graphics.Dispose();
                }
            }

            if (!_oldPal.IsNull && !_dc.IsNull)
            {
                Gdi32.SelectPalette(_dc, _oldPal, BOOL.FALSE);
                _oldPal = default;
            }
        }
        /// <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;
        }
Beispiel #8
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;
        }