System.Drawing.Graphics.SetClip C# (CSharp) Method

SetClip() public method

public SetClip ( Region region, CombineMode combineMode ) : void
region Region
combineMode CombineMode
return void
        public void SetClip(Region region, CombineMode combineMode)
        {
            // We need to reset the clip that is active now so that the graphic
            // states are correct when we set them.
            ResetClip ();

            switch (combineMode)
            {
            case CombineMode.Replace:
                // Set our clip region by cloning the region that is passed for now
                clipRegion = region.Clone ();
                break;
            case CombineMode.Intersect:

                clipRegion.Intersect (region);

                break;
            case CombineMode.Union:

                clipRegion.Union (region);

                break;
            case CombineMode.Exclude:

                clipRegion.Exclude (region);

                break;
            case CombineMode.Xor:

                clipRegion.Xor (region);

                break;
            default:
                throw new NotImplementedException ("SetClip for CombineMode " + combineMode + " not implemented");
            }

            //Unlike the current path, the current clipping path is part of the graphics state.
            //Therefore, to re-enlarge the paintable area by restoring the clipping path to a
            //prior state, you must save the graphics state before you clip and restore the graphics
            //state after you’ve completed any clipped drawing.
            context.SaveState ();
            if (clipRegion.IsEmpty) {
                context.ClipToRect (CGRect.Empty);
            } else {
                //context.ClipToRect ((RectangleF)clipRegion.regionObject);
                context.AddPath (clipRegion.regionPath);
                context.ClosePath ();
                context.Clip ();
            }
            clipSet++;
        }

Same methods

Graphics::SetClip ( Graphics g ) : void
Graphics::SetClip ( Graphics g, CombineMode combineMode ) : void
Graphics::SetClip ( GraphicsPath graphicsPath ) : void
Graphics::SetClip ( GraphicsPath graphicsPath, CombineMode combineMode ) : void
Graphics::SetClip ( Rectangle rect ) : void
Graphics::SetClip ( Rectangle rect, CombineMode combineMode ) : void
Graphics::SetClip ( RectangleF rect ) : void
Graphics::SetClip ( RectangleF rect, CombineMode combineMode ) : void

Usage Example

Example #1
2
        public override void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, RenderMode renderMode)
        {
            Rectangle applyRect = ImageHelper.CreateIntersectRectangle(applyBitmap.Size, rect, Invert);

            if (applyRect.Width == 0 || applyRect.Height == 0)
            {
                // nothing to do
                return;
            }
            GraphicsState state = graphics.Save();
            if (Invert)
            {
                graphics.SetClip(applyRect);
                graphics.ExcludeClip(rect);
            }
            ColorMatrix grayscaleMatrix = new ColorMatrix(new[] {
                new[] {.3f, .3f, .3f, 0, 0},
                new[] {.59f, .59f, .59f, 0, 0},
                new[] {.11f, .11f, .11f, 0, 0},
                new float[] {0, 0, 0, 1, 0},
                new float[] {0, 0, 0, 0, 1}
            });
            using (ImageAttributes ia = new ImageAttributes())
            {
                ia.SetColorMatrix(grayscaleMatrix);
                graphics.DrawImage(applyBitmap, applyRect, applyRect.X, applyRect.Y, applyRect.Width, applyRect.Height, GraphicsUnit.Pixel, ia);
            }
            graphics.Restore(state);
        }
All Usage Examples Of System.Drawing.Graphics::SetClip