Platforms to show: All Mac Windows Linux Cross-Platform

### CGContextMBS class

Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |

class | CoreGraphics | MBS MacCG Plugin | 2.7 | ✅ Yes | ❌ No | ❌ No | ✅ Yes | All |

**Function:**A class for a core graphics context.

**Example:**

`Dim p As New Picture(200, 200)`

Dim g As Graphics = p.Graphics

// Clear image with white

g.ForeColor = &cFFFFFF

g.FillRect 0, 0, g.Width, g.Height

Dim c As New CGContextMBS(g)

c.SetRGBFillColor(1, 0, 0)

// coordinates are swapped in CGContext, so y = 20 means 20 pixels from bottom of picture!

Const x = 10

Const y = 20

Const w = 120

Const h = 100

c.FillRect New CGRectMBS(x, y, w, h)

Self.Backdrop = p

**Notes:**

If the handle property has a non zero value, the destructor of this class will release the context reference.

If you create this in a method based on a window, please release it within that method. e.g. in a paint event, create it and let RB delete the last reference on the end of the method. Else you may see crashes as the context is still being around while the graphics port has been released.

- 5 properties
- property handle as Integer
- property InterpolationQuality as Integer
- property RetainCount as Integer
- property TextMatrix as CGAffineTransformMBS
- property TextPosition as CGPointMBS

- 103 methods
- method AddArc(x as Double, y as Double, radius as Double, startangle as Double, endangle as Double, clockwise as boolean)
- method addArcToPath(x as Double, y as Double, w as Double, h as Double, startAngle as Integer, arcAngle as Integer)
- method AddArcToPoint(x1 as Double, y1 as Double, x2 as Double, y2 as Double, radius as Double)
- method AddCurveToPoint(cp1x as Double, cp1y as Double, cp2x as Double, cp2y as Double, x as Double, y as Double)
- method AddEllipseInRect(r as CGRectMBS)
- method AddLines(p() as CGPointMBS)
- method AddLineToPoint(x as Double, y as Double)
- method addOvalToPath(x as Double, y as Double, w as Double, h as Double)
- method AddPath(path as CGPathMBS)
- method AddQuadCurveToPoint(cpx as Double, cpy as Double, x as Double, y as Double)
- method AddRect(r as CGRectMBS)
- method AddRects(r() as CGRectMBS)
- method addRoundedRectToPath(x as Double, y as Double, w as Double, h as Double, arcWidth as Double, arcHeight as Double)
- method BeginPage(mediabox as CGRectMBS)
- method BeginPath
- method BeginTransparencyLayer(auxiliaryInfo as Dictionary = nil)
- method BeginTransparencyLayerWithRect(r as CGRectMBS, auxiliaryInfo as Dictionary = nil)
- method clearRect(rect as CGRectMBS)
- method clip
- method ClipToMask(rect as CGRectMBS, mask as CGImageMBS)
- method clipToRect(rect as CGRectMBS)
- method Close
- method closePath
- method ConcatCTM(transform as CGAffineTransformMBS)
- method Constructor(g as graphics)
- method Constructor(handle as Integer)
- method Constructor(pic as Picture)
- method CopyPath as CGPathMBS
- method DrawCGPDFDocument(pdf as Variant, rect as CGRectMBS, page as Integer)
- method DrawLayerAtPoint(Point as CGPointMBS, layer as CGLayerMBS)
- method DrawLayerInRect(rect as CGRectMBS, layer as CGLayerMBS)
- method DrawLinearGradient(gradient as CGGradientMBS, startPoint as CGPointMBS, endPoint as CGPointMBS, options as Integer)
- method DrawPath(mode as Integer)
- method DrawPicture(pic as CGImageMBS, rect as CGRectMBS)
- method DrawRadialGradient(gradient as CGGradientMBS, startCenter as CGPointMBS, startRadius as Double, endCenter as CGPointMBS, endRadius as Double, options as Integer)
- method DrawShading(shading as CGShadingMBS)
- method DrawTiledImage(pic as CGImageMBS, rect as CGRectMBS)
- method EndPage
- method EndTransparencyLayer
- method EOClip
- method EOFillPath
- method FillEllipseInRect(rect as CGRectMBS)
- method FillPath
- method FillRect(rect as CGRectMBS)
- method fillRoundedRect(x as Double, y as Double, w as Double, h as Double, arcWidth as Double, arcHeight as Double)
- method Flush
- method frameArc(x as Double, y as Double, w as Double, h as Double, startAngle as Integer, arcAngle as Integer)
- method frameOval(x as Double, y as Double, w as Double, h as Double)
- method frameRect(x as Double, y as Double, w as Double, h as Double)
- method GetClipBoundingBox as CGRectMBS
- method GetCTM as CGAffineTransformMBS
- method GetPathBoundingBox as CGRectMBS
- method GetPathCurrentPoint as CGPointMBS
- method GetTextPosition as CGPointMBS
- method IsPathEmpty as boolean
- method MoveToPoint(x as Double, y as Double)
- method paintArc(x as Double, y as Double, w as Double, h as Double, startAngle as Integer, arcAngle as Integer)
- method paintOval(x as Double, y as Double, w as Double, h as Double)
- method paintRect(x as Double, y as Double, w as Double, h as Double)
- method PathContainsPoint(point as CGPointMBS, mode as Integer) as boolean
- method ReplacePathWithStrokedPath
- method RestoreGState
- method RotateCTM(angle as Double)
- method SaveGState
- method ScaleCTM(sx as Double, sy as Double)
- method SelectFont(name as string, size as Double, fontencoding as Integer)
- method SetAllowsAntialiasing(allowsAntialiasing as boolean)
- method SetAlpha(alpha as Double)
- method SetBlendMode(BlendMode as Integer)
- method SetCharacterSpacing(spacing as Double)
- method SetCMYKFillColor(cyan as Double, magenta as Double, yellow as Double, black as Double, alpha as Double = 1.0)
- method SetCMYKStrokeColor(cyan as Double, magenta as Double, yellow as Double, black as Double, alpha as Double = 1.0)
- method SetFillColor(color as CGColorMBS)
- method SetFillColorSpace(colorspace as CGColorSpaceMBS)
- method SetFlatness(flatness as Double)
- method SetFont(font as CGFontMBS)
- method SetFontSize(size as Double)
- method SetGrayFillColor(gray as Double, alpha as Double = 1.0)
- method SetGrayStrokeColor(gray as Double, alpha as Double = 1.0)
- method SetLineCap(cap as Integer)
- method SetLineDash(phase as Double, lengths as memoryblock, count as Integer)
- method SetLineJoin(join as Integer)
- method SetLineWidth(width as Double)
- method SetMiterLimit(limit as Double)
- method SetRenderingIntent(intent as Integer)
- method SetRGBFillColor(red as Double, green as Double, blue as Double, alpha as Double = 1.0)
- method SetRGBStrokeColor(red as Double, green as Double, blue as Double, alpha as Double = 1.0)
- method SetShadow(x as Double, y as Double, blur as Double)
- method SetShadowWithColor(x as Double, y as Double, blur as Double, colorvalue as CGColorMBS)
- method SetShouldAntialias(shouldAntialias as boolean)
- method SetShouldSmoothFonts(shouldSmoothFonts as boolean)
- method SetStrokeColor(color as CGColorMBS)
- method SetStrokeColorSpace(colorspace as CGColorSpaceMBS)
- method SetTextDrawingMode(mode as Integer)
- method ShowText(text as string)
- method ShowTextAtPoint(text as string, x as Double, y as Double)
- method StrokeEllipseInRect(rect as CGRectMBS)
- method StrokePath
- method StrokeRect(rect as CGRectMBS)
- method StrokeRectWithWidth(rect as CGRectMBS, width as Double)
- method strokeRoundedRect(x as Double, y as Double, w as Double, h as Double, arcWidth as Double, arcHeight as Double)
- method Synchronize
- method TranslateCTM(tx as Double, ty as Double)

- 2 shared methods
- shared method contextWithCGContext(handle as Integer) as CGContextMBS
- shared method contextWithCGContext(handle as Ptr) as CGContextMBS

- 54 constants

#### Sub classes:

#### Some useful global functions for this class:

- GetCurrentCGContextMBS as CGContextMBS
- NewCIContextMBS(cgcontext as CGContextMBS) as CIContextMBS
- NewCIContextMBS(cgcontext as CGContextMBS, OutputColorSpace as CGColorSpaceMBS, WorkingColorSpace as CGColorSpaceMBS, UseSoftwareRenderer as Boolean) as CIContextMBS

#### Some methods using this class:

- CIContextMBS.Constructor(cgcontext as CGContextMBS, OutputColorSpace as CGColorSpaceMBS, WorkingColorSpace as CGColorSpaceMBS, UseSoftwareRenderer as Boolean)
- CTFontMBS.Draw(glyphs() as Integer, positions() as CGPointMBS, context as CGContextMBS)
- CTFrameMBS.Draw(context as CGContextMBS)
- CTRunMBS.ImageBounds(context as CGContextMBS, location as Integer, length as Integer) as CGRectMBS
- MKOverlayPathRendererMBS.applyStrokeProperties(context as CGContextMBS, zoomScale as double)
- MKOverlayPathRendererMBS.fillPath(path as CGPathMBS, context as CGContextMBS)
- MKOverlayPathRendererMBS.strokePath(path as CGPathMBS, context as CGContextMBS)
- MKOverlayRendererMBS.drawMapRect(Rect as MKMapRectMBS, zoomScale as Double, context as CGContextMBS)
- QDPictMBS.DrawToCGContext(context as CGContextMBS, r as CGRectMBS)
- Window.CGContextMBS as CGContextMBS

#### Some properties using for this class:

- CIContextMBS.CGContext as CGContextMBS
- CVImageBufferMBS.Context as CGContextMBS

#### Some events using this class:

- MKCustomOverlayRendererMBS.DrawMapRect(mapRect as MKMapRectMBS, zoomScale as Double, context as CGContextMBS)

#### Some examples using this class:

- /AVFoundation/Transparent Player
- /MacCG/Carbon Print Manager/sample with sheets
- /MacCG/CoreGraphics DirectDisplay/Capture Screen with CGContextMBS
- /MacCG/CoreGraphics PDF/PDF Transparency
- /MacCG/CoreGraphics PDF/Show PDF
- /MacCG/CoreGraphics PDF/Simple Make PDF
- /MacCG/CoreText/CoreText Test
- /MacCG/Quartz Gradients
- /MacCG/Rotate PDF Page
- /VLC/Show two videos

#### Blog Entries

- MBS Xojo Plugins, version 20.3pr3
- MBS Xojo Plugins, version 19.6pr3
- Colorspaces in MacOS with Xojo
- MBS Xojo / Real Studio Plugins, version 14.2pr8
- MBS Real Studio Plugins, version 13.1pr8
- MBS Real Studio Plugins, version 13.1pr3
- MBS Real Studio Plugins, version 12.1pr10
- MBS REALbasic Plugins, version 11.2pr1
- MBS REALbasic Plugins, version 11.1pr4
- MonkeyBread Software Releases the MBS REALbasic plug-ins 8.4

#### Xojo Developer Magazine

The items on this page are in the following plugins: MBS MacCG Plugin.

Links

MBS Xojo Chart Plugins