CGContext Class

Definition

Graphics context and primitives to draw in them.

public class CGContext : IDisposable, ObjCRuntime.INativeObject
type CGContext = class
    interface INativeObject
    interface IDisposable
Inheritance
CGContext
Derived
Implements

Remarks

A CGContext is a Quartz 2D destination for drawing. It holds parameters and their states (set with functions such as M:CoreGraphics.CGContext.SetFillColor*) and device information (for instance, ConvertSizeToDeviceSpace(CGSize)). 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:

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 Draw(CGRect) method in a custom UIView and the use of GetImageFromCurrentImageContext() to retrieve the current CGContext on which to draw.

Constructors

CGContext(IntPtr)

Creates a CGContext from an unmanaged CGContextRef handle.

Properties

Handle

Handle (pointer) to the unmanaged object representation.

InterpolationQuality

A hint for the level of quality used when interpolating images (for example, when scaling).

TextMatrix

Defines the transform between text space and user space. Independent of the CGContext's state.

TextPosition

The location, in user space coordinates, at which to draw text.

Methods

AddArc(nfloat, nfloat, nfloat, nfloat, nfloat, Boolean)
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 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()

Casts the CGContext into a CGBitmapContext.

BeginPage(Nullable<CGRect>)
BeginPath()

Starts a new path in the graphics context.

BeginTransparencyLayer(CGRect, NSDictionary)
BeginTransparencyLayer(NSDictionary)

With 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(CGRect, CGImage)
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)

Converts a point from device space coordinates to user space coordinates.

ConvertRectToDeviceSpace(CGRect)

Converts a rectangle from user space coordinates to device space coordinates.

ConvertRectToUserSpace(CGRect)

Converts a rectangle from device space coordinates to user space coordinates.

ConvertSizeToDeviceSpace(CGSize)

Converts a size from user space coordinates to device space coordinates.

ConvertSizeToUserSpace(CGSize)

Converts a size from device space coordinates to user space coordinates.

CopyPath()

Returns a deep copy of the current path in the current context.

Dispose()

Releases the resources used by the CGContext object.

Dispose(Boolean)

Releases the resources used by the CGContext object.

DrawImage(CGRect, CGImage)
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, CGPoint, CGPoint, CGGradientDrawingOptions)
DrawPath(CGPathDrawingMode)

Draws the CGContext's current path.

DrawPDFPage(CGPDFPage)

Renders the specified PDF page.

DrawRadialGradient(CGGradient, CGPoint, nfloat, CGPoint, nfloat, CGGradientDrawingOptions)
DrawShading(CGShading)

Renders the specified shading.

DrawTiledImage(CGRect, CGImage)
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()

The bounding box of the current clipping path.

GetCTM()

Retrieves the current Context Transformation Matrix.

GetPathBoundingBox()

Returns the bounding box for the current path.

GetPathCurrentPoint()

The current point in the CGContext's path.

GetUserSpaceToDeviceSpaceTransform()

The CGAffineTransform that maps user-space coordinates into device-space coordinates.

IsPathEmpty()

Whether the current path contains any subpaths.

MoveTo(nfloat, nfloat)

Begins a sub-path at the specified position.

PathContainsPoint(CGPoint, CGPathDrawingMode)
PointToDeviceSpace(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.

ResetClip()
RestoreState()

Sets the state of the CGContext to what it was when 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 RestoreState().)

ScaleCTM(nfloat, nfloat)

Scales the current transformation matrix.

SelectFont(String, nfloat, CGTextEncoding)

Developers should not use this deprecated method. Developers should use the 'CoreText' API instead.

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, 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.

SetFillColor(nfloat[])

Sets the fill color. SetFillColorSpace(CGColorSpace) must be called prior.

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(nfloat, nfloat[], Int32)
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 SetAllowsAntialiasing(Boolean) to enable or disable antialiasing.

SetShouldSmoothFonts(Boolean)

Used in conjunction with SetAllowsFontSmoothing(Boolean) to enable or disable font smoothing.

SetShouldSubpixelPositionFonts(Boolean)

Used in conjunction with SetAllowsSubpixelPositioning(Boolean) to enable or disable glyph alignment with pixels.

SetStrokeColor(CGColor)

Sets the stroke color.

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.

SetStrokeColor(nfloat[])

Sets the stroke color. This API has undefined behavior if you do not set the colorspace beforehand.

SetStrokeColorSpace(CGColorSpace)

Sets the CGColorSpace to be used with M:CoreGraphics.CGContext.SetStrokeColor(float[]).

SetStrokePattern(CGPattern, nfloat[])

Sets the stroke pattern of the context.

SetTextDrawingMode(CGTextDrawingMode)

Specifies how glyphs should be rendered.

ShouldSubpixelQuantizeFonts(Boolean)

With SetAllowsFontSubpixelQuantization(Boolean), determines whether fonts should be drawn at subpixel locations.

ShowGlyphs(UInt16[])

This method has been deprecated in favor of N:CoreText.

ShowGlyphs(UInt16[], Int32)

This method has been deprecated in favor of N:CoreText.

ShowGlyphsAtPoint(nfloat, nfloat, UInt16[])
ShowGlyphsAtPoint(nfloat, nfloat, UInt16[], Int32)
ShowGlyphsAtPositions(UInt16[], CGPoint[], Int32)
ShowGlyphsWithAdvances(UInt16[], CGSize[], Int32)
ShowText(Byte[])

This method has been deprecated in favor of N:CoreText.

ShowText(Byte[], Int32)

This method has been deprecated in favor of N:CoreText.

ShowText(String)

This method has been deprecated in favor of N:CoreText.

ShowText(String, Int32)

This method has been deprecated in favor of N:CoreText.

ShowTextAtPoint(nfloat, nfloat, Byte[])
ShowTextAtPoint(nfloat, nfloat, Byte[], Int32)
ShowTextAtPoint(nfloat, nfloat, String)
ShowTextAtPoint(nfloat, nfloat, String, Int32)
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.

Applies to

See also