Platforms to show: All Mac Windows Linux Cross-Platform

Back to CGContextMBS class.

CGContextMBS.contextWithCGContext(handle as Integer) as CGContextMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method CoreGraphics MBS MacCG Plugin 13.1 ✅ Yes ❌ No ❌ No ✅ Yes All
Creates a new CGContextMBS object based on a CGContextRef.
Example
// load a picture
dim f as FolderItem = SpecialFolder.Desktop.Child("ColorSpin.jpg")
dim pic as Picture = picture.Open(f)

// open printer
dim g as Graphics = OpenPrinterDialog
if g = nil then Return

// draw
g.DrawPicture pic, 0, 0

// now load again
dim ImageSource as new CGImageSourceMBS(f)
dim img as CGImageMBS = ImageSource.CreateImageAtIndex(0)
dim cs as CGColorSpaceMBS = CGColorSpaceMBS.CreateDeviceRGB

// copy with replacing colorspace
img = img.CopyWithColorSpace(cs)

// and draw
#If XojoVersion >= 2019.02 Then
Dim p As ptr = g.Handle(Graphics.HandleTypes.CGContextRef)
Dim c As CGContextMBS = CGContextMBS.contextWithCGContext(p)
#Else
Dim h As Integer = g.Handle(g.HandleTypeCGContextRef)
Dim c As CGContextMBS = CGContextMBS.contextWithCGContext(h)
#EndIf
dim r as CGRectMBS = CGMakeRectMBS(0, 0, img.Width, img.Height)

c.DrawPicture(img, r)
c.Flush

The CGContext is retained.
Returns nil on any error.

See also:

Some examples using this method:

CGContextMBS.contextWithCGContext(handle as Ptr) as CGContextMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method CoreGraphics MBS MacCG Plugin 20.0 ✅ Yes ❌ No ❌ No ✅ Yes All
Creates a new CGContextMBS object based on a CGContextRef.
Example
// load a picture
dim f as FolderItem = SpecialFolder.Desktop.Child("ColorSpin.jpg")
dim pic as Picture = picture.Open(f)

// open printer
dim g as Graphics = OpenPrinterDialog
if g = nil then Return

// draw
g.DrawPicture pic, 0, 0

// now load again
dim ImageSource as new CGImageSourceMBS(f)
dim img as CGImageMBS = ImageSource.CreateImageAtIndex(0)
dim cs as CGColorSpaceMBS = CGColorSpaceMBS.CreateDeviceRGB

// copy with replacing colorspace
img = img.CopyWithColorSpace(cs)

// and draw
#If XojoVersion >= 2019.02 Then
Dim p As ptr = g.Handle(Graphics.HandleTypes.CGContextRef)
Dim c As CGContextMBS = CGContextMBS.contextWithCGContext(p)
#Else
Dim h As Integer = g.Handle(g.HandleTypeCGContextRef)
Dim c As CGContextMBS = CGContextMBS.contextWithCGContext(h)
#EndIf
dim r as CGRectMBS = CGMakeRectMBS(0, 0, img.Width, img.Height)

c.DrawPicture(img, r)
c.Flush

The CGContext is retained.
Returns nil on any error.

See also:

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


The biggest plugin in space...