CoreGraphics.CGContext Class
Graphics context and primitives to draw in them.

See Also: CGContext Members

Syntax

public class CGContext : ObjCRuntime.INativeObject, IDisposable

Remarks

A CoreGraphics.CGContext is a Quartz 2D destination for drawing. It holds parameters and their states (set with functions such as CGContext.SetFillColor) and device information (for instance, CGContext.ConvertSizeToDeviceSpace). CoreGraphics.CGContexts may represent a screen area, a bitmap, a PDF document, or a printer.

There are a limited number of CoreGraphics.CGContexts available and application developer's should be conscientious about disposing of them after use. Generally, that means putting the drawing code in using blocks, as shown in the following example:

C# Example

public override void Draw (RectangleF rect)
{
    base.Draw (rect);
    using (var context = UIGraphics.GetCurrentContext ())
    {
        // draw a rectangle using a path
	var myRectangleButtonPath = new CGPath ();
	myRectangleButtonPath.AddRect (new RectangleF (new PointF (100, 10), new SizeF (200, 400)));
	context.AddPath (myRectangleButtonPath);
	context.DrawPath (CGPathDrawingMode.Stroke);
    }
}          
          

The example additionally shows the very common pattern of an overridden UIKit.UIView.Draw method in a custom UIKit.UIView and the use of UIKit.UIGraphics.GetCurrentContext to retrieve the current CoreGraphics.CGContext on which to draw.

Related content

Requirements

Namespace: CoreGraphics
Assembly: Xamarin.iOS (in Xamarin.iOS.dll)
Assembly Versions: 0.0.0.0