iOS Guides | Samples

CoreGraphics.CGContext Class

Graphics context and primitives to draw in them.

See Also: CGContext

Syntax

public class CGContext : INativeObject, IDisposable

Remarks

A 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). CGContexts may represent a screen area, a bitmap, a PDF document, or a printer.

There are a limited number of 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 UIView.Draw method in a custom UIView and the use of UIGraphics.GetCurrentContext to retrieve the current CGContext on which to draw.

Related content

Requirements

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

The members of CoreGraphics.CGContext are listed below.

See Also: Object

Public Constructors

Creates a CGContext from an unmanaged CGContextRef handle.

Public Properties

[read-only]
HandleIntPtr. Handle (pointer) to the unmanaged object representation.
InterpolationQualityCGInterpolationQuality. A hint for the level of quality used when interpolating images (for example, when scaling).
TextMatrixCGAffineTransform. Defines the transform between text space and user space. Independent of the CGContext's state.
TextPositionCGPoint. The location, in user space coordinates, at which to draw text.

Public Methods

AddArc(Boolean, nfloat, nfloat, nfloat, nfloat, nfloat)
Adds an arc to the current path.
AddArcToPoint(nfloat, nfloat, nfloat, nfloat, nfloat)
Adds an arc to the current path.
AddCurveToPoint(nfloat, nfloat, nfloat, nfloat, nfloat, nfloat)
Adds a cubic Bézier curve at the current point, with the specified control parameters.
AddEllipseInRect(CGRect)
Adds an ellipse that fits in the specified rect.
AddLines(CGPoint[])
Adds the given lines to the current path.
AddLineToPoint(nfloat, nfloat)
Appends a line from the current point (see CGContext.GetPathCurrentPoint) to the specified coordinates in user space.
AddPath(CGPath)
Adds the specified path to the current path.
AddQuadCurveToPoint(nfloat, nfloat, nfloat, nfloat)
Adds a quadratic Bézier curve to the current path.
AddRect(CGRect)
Adds a rectangular path to the current path.
AddRects(CGRect[])
Adds an array of rectangular paths to the current path.
AsBitmapContext() : CGBitmapContext
Casts the CGContext into a CGBitmapContext.
BeginPage(Nullable<CGRect>)
BeginPath()
Starts a new path in the graphics context.
BeginTransparencyLayer(NSDictionary)
With CGContext.EndTransparencyLayer, encloses operations on a fully transparent layer.
BeginTransparencyLayer(NSDictionary, CGRect)
With CGContext.EndTransparencyLayer, encloses operations on a fully transparent layer.
ClearRect(CGRect)
Paints the rectangle transparently.
Clip()
Sets the current path of the graphics context to be the clipping path.
ClipToMask(CGImage, CGRect)
Modifies the clipping area to the intersection of the current clipping path with mask mapped into the rect.
ClipToRect(CGRect)
Modifies the clipping path to be the intersection of the current path and the supplied rectangle.
ClipToRects(CGRect[])
Modifies the current clipping path to be the insersection of the current clipping path and the region specified by the supplied rectangles.
ClosePath()
Closes and completes the current path.
ConcatCTM(CGAffineTransform)
Concatenates the specified transform onto the current transformation matrix.
ContextFillRects(CGRect[])
ConvertPointToUserSpace(CGPoint) : CGPoint
Converts a point from device space coordinates to user space coordinates.
ConvertRectToDeviceSpace(CGRect) : CGRect
Converts a rectangle from user space coordinates to device space coordinates.
ConvertRectToUserSpace(CGRect) : CGRect
Converts a rectangle from device space coordinates to user space coordinates.
ConvertSizeToDeviceSpace(CGSize) : CGSize
Converts a size from user space coordinates to device space coordinates.
ConvertSizeToUserSpace(CGSize) : CGSize
Converts a size from device space coordinates to user space coordinates.
CopyPath() : CGPath
Returns a deep copy of the current path in the current context.
Dispose()
Releases the resources used by the CGContext object.
DrawImage(CGImage, CGRect)
Draws an image into the graphics context bounded to the specified rectangle.
DrawLayer(CGLayer, CGPoint)
Draws a layer into the graphics context at the specified point.
DrawLayer(CGLayer, CGRect)
Draws a layer into the graphics context bounded by the specified rectangle.
DrawLinearGradient(CGGradient, CGGradientDrawingOptions, CGPoint, CGPoint)
Draws the specified gradient such that lines perpendicular to that between startPoint and endPoint will be the same color.
DrawPath(CGPathDrawingMode)
Draws the CGContext's current path.
DrawPDFPage(CGPDFPage)
Renders the specified PDF page.
DrawRadialGradient(CGGradient, CGGradientDrawingOptions, CGPoint, nfloat, CGPoint, nfloat)
Draws a radial gradient using the specified parameters.
DrawShading(CGShading)
Renders the specified shading.
DrawTiledImage(CGImage, CGRect)
Tiles the clipping region with the specified image.
EndPage()
Called to indicate the end of a page in a page-based context.
EndTransparencyLayer()
Indicates the end of a transparency layer.
EOClip()
Modifies the current clipping path, using the Even-Odd rule.
EOFillPath()
Fills the current path, using the Even-Odd rule.
FillEllipseInRect(CGRect)
Paints the ellipse defined by rect. Afterwards, the current path is reset.
FillPath()
Fills the current path, using Non-Zero Winding rule.
FillRect(CGRect)
Paints the specified rect.
Finalize()
Finalizer for the CGContext object
Flush()
Forces all pending drawing to be rendered.
GetClipBoundingBox() : CGRect
The bounding box of the current clipping path.
GetCTM() : CGAffineTransform
Retrieves the current Context Transformation Matrix.
GetPathBoundingBox() : CGRect
Returns the bounding box for the current path.
GetPathCurrentPoint() : CGPoint
The current point in the CGContext's path.
GetUserSpaceToDeviceSpaceTransform() : CGAffineTransform
The CGAffineTransform that maps user-space coordinates into device-space coordinates.
IsPathEmpty() : Boolean
Whether the current path contains any subpaths.
MoveTo(nfloat, nfloat)
Begins a sub-path at the specified position.
PathContainsPoint(CGPathDrawingMode, CGPoint) : Boolean
Whether the point is inside the painted region if the path is stroked with the modeCGPathDrawingMode.
PointToDeviceSpace(CGPoint) : CGPoint
Returns a new PointF that converts the user-space point into device space.
ReplacePathWithStrokedPath()
Replaces the current path with the stroked version of the path, based on the stroke paremeters.
RestoreState()
Sets the state of the CGContext to what it was when CGContext.SaveState was last called.
RotateCTM(nfloat)
Rotates the current transformation matrix by angle radians, with positive values rotating counterclockwise.
SaveState()
Stores the state of the CGContext. (See CGContext.RestoreState.)
ScaleCTM(nfloat, nfloat)
Scales the current transformation matrix.
SelectFont(String, CGTextEncoding, nfloat)
Application developers should use CoreText instead of this deprecated method, which selects a particular font.
SetAllowsAntialiasing(Boolean)
Whether the context allows antialiasing.
SetAllowsFontSmoothing(Boolean)
Whether the context allows font smoothing.
SetAllowsFontSubpixelQuantization(Boolean)
Whether the context allows for glyphs to be drawn at subpixel locations.
SetAllowsSubpixelPositioning(Boolean)
Whether the context allows for glyphs to be aligned other than to pixel boundaries.
SetAlpha(nfloat)
Sets the opacity for drawing, in the range 0.0 (transparent) to 1.0 (opaque).
SetBlendMode(CGBlendMode)
Specifies the compositing mode.
SetCharacterSpacing(nfloat)
Adds additional spacing between glyphs.
SetFillColor(CGColor)
Sets the fill color to the specified CGColor.
SetFillColor(nfloat[])
Sets the fill color. CGContext.SetFillColorSpace must be called prior.
SetFillColor(nfloat, nfloat)
Sets the fill color to a gray level, in the range 0 (black) to 1 (white).
SetFillColor(nfloat, nfloat, nfloat, nfloat)
Sets the fill color to the specified RGBA values.
SetFillColor(nfloat, nfloat, nfloat, nfloat, nfloat)
Sets the fill color using CMYK color-space components, plus an alpha value.
SetFillColorSpace(CGColorSpace)
Specifies the CGColorSpace to be used in the context.
SetFillPattern(CGPattern, nfloat[])
Sets the fill pattern of the context.
SetFlatness(nfloat)
The accuracy of curved paths.
SetFont(CGFont)
Sets the CGFont used to render text.
SetFontSize(nfloat)
Sets the size of the font used to rende text.
SetLineCap(CGLineCap)
Sets the style for the ends of lines.
SetLineDash(nfloat, nfloat[])
The pattern to be used for drawing dashed lines.
SetLineDash(Int32, nfloat, nfloat[])
The pattern to be used for drawing dashed lines.
SetLineJoin(CGLineJoin)
Sets the way lines are joined.
SetLineWidth(nfloat)
Sets the width of the stroked line. The line extends for half w to either side of the path.
SetMiterLimit(nfloat)
Specifies the miter limit for connecting lines.
SetPatternPhase(CGSize)
Translates the pattern prior to beginning to tile it.
SetRenderingIntent(CGColorRenderingIntent)
How colors outside the destination color space are handled.
SetShadow(CGSize, nfloat, CGColor)
SetShouldAntialias(Boolean)
Used in conjunction with CGContext.SetAllowsAntialiasing to enable or disable antialiasing.
SetShouldSmoothFonts(Boolean)
Used in conjunction with CGContext.SetAllowsFontSmoothing to enable or disable font smoothing.
SetShouldSubpixelPositionFonts(Boolean)
Used in conjunction with CGContext.SetAllowsSubpixelPositioning to enable or disable glyph alignment with pixels.
SetStrokeColor(CGColor)
Sets the stroke color.
SetStrokeColor(nfloat[])
Sets the stroke color. This API has undefined behavior if you do not set the colorspace beforehand.
SetStrokeColor(nfloat, nfloat)
Sets the stroke color to a grayscale value.
SetStrokeColor(nfloat, nfloat, nfloat, nfloat)
Sets the stroke color to an RGBA color.
SetStrokeColor(nfloat, nfloat, nfloat, nfloat, nfloat)
Sets the stroke color to a CMYKA color.
SetStrokeColorSpace(CGColorSpace)
Sets the CGColorSpace to be used with CGContext.SetStrokeColor(float[]).
SetStrokePattern(CGPattern, nfloat[])
Sets the stroke pattern of the context.
SetTextDrawingMode(CGTextDrawingMode)
Specifies how glyphs should be rendered.
ShouldSubpixelQuantizeFonts(Boolean)
With CGContext.SetAllowsFontSubpixelQuantization, determines whether fonts should be drawn at subpixel locations.
ShowGlyphs(UInt16[])
This method has been deprecated in favor of CoreText.
ShowGlyphs(UInt16[], Int32)
This method has been deprecated in favor of CoreText.
ShowGlyphsAtPoint(UInt16[], nfloat, nfloat)
This method has been deprecated in favor of CoreText.
ShowGlyphsAtPoint(UInt16[], Int32, nfloat, nfloat)
This method has been deprecated in favor of CoreText.
ShowGlyphsAtPositions(UInt16[], Int32, CGPoint[])
This method has been deprecated in favor of CoreText..
ShowGlyphsWithAdvances(UInt16[], Int32, CGSize[])
This method has been deprecated in favor of CoreText.
ShowText(Byte[])
This method has been deprecated in favor of CoreText.
ShowText(String)
This method has been deprecated in favor of CoreText.
ShowText(Byte[], Int32)
This method has been deprecated in favor of CoreText.
ShowText(String, Int32)
This method has been deprecated in favor of CoreText.
ShowTextAtPoint(Byte[], nfloat, nfloat)
This method has been deprecated in favor of CoreText.
ShowTextAtPoint(String, nfloat, nfloat)
This method has been deprecated in favor of CoreText.
ShowTextAtPoint(Byte[], Int32, nfloat, nfloat)
This method has been deprecated in favor of CoreText.
ShowTextAtPoint(String, Int32, nfloat, nfloat)
This method has been deprecated in favor of CoreText.
StrokeEllipseInRect(CGRect)
Strokes the ellipse defined by rect. Afterwards, the current path is reset.
StrokeLineSegments(CGPoint[])
Strokes the lines defined by the pairs in points. Afterwards, the current path is reset.
StrokePath()
Strokes the current path. Afterwards, the current path is reset.
StrokeRect(CGRect)
Strokes the specified rect. Afterwards, the current path is reset.
StrokeRectWithWidth(CGRect, nfloat)
Strokes the rect. The line extends half of width to either side of the rect perimeter.
Synchronize()
Marks a CGContext for update.
TranslateCTM(nfloat, nfloat)
Changes the origin of the user coordinate system.

Protected Methods

Dispose(Boolean)
Releases the resources used by the CGContext object.