Platforms to show: All Mac Windows Linux Cross-Platform

Back to NSGraphicsMBS class.

NSGraphicsMBS.graphicsContext as NSGraphicsMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method Cocoa Drawing MBS MacCocoa Plugin 13.1 ✅ Yes ❌ No ❌ No ✅ Yes All
Creates a NSGraphicsMBS object with the current graphics context of the current thread.
Example
Sub Paint(g As Graphics)
// Canvas Paint event in a Mac Cocoa application

dim ng as NSGraphicsMBS = NSGraphicsMBS.graphicsContext
ng.drawAtPoint "Hello World", new NSPointMBS(30, 30)

End Sub

Returns nil on any error.

Some examples using this method:

NSGraphicsMBS.graphicsContextWithCGContext(targetCGContext as Variant, initialFlippedState as boolean = false) as NSGraphicsMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method Cocoa Drawing MBS MacCocoa Plugin 13.1 ✅ Yes ❌ No ❌ No ✅ Yes All
Creates a new graphics context pointing to the given CGContextMBS object.
Example
Sub Paint(g As Graphics)
// get current context
dim cg as CGContextMBS = GetCurrentCGContextMBS

// get graphics context
dim ng as NSGraphicsMBS = NSGraphicsMBS.graphicsContextWithCGContext(cg)

// and draw inside
ng.drawAtPoint "Hello World", new NSPointMBS(30, 30)
End Sub

Returns nil on any error.

Some examples using this method:

NSGraphicsMBS.graphicsContextWithCGContextHandle(targetCGContextRef as Integer, initialFlippedState as boolean = false) as NSGraphicsMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method Cocoa Drawing MBS MacCocoa Plugin 15.0 ✅ Yes ❌ No ❌ No ✅ Yes All
Creates a new graphics context pointing to the given CGContext reference.
Example
Sub Paint(g As Graphics)
// get current context
dim cg as CGContextMBS = GetCurrentCGContextMBS

// get graphics context
dim ng as NSGraphicsMBS = NSGraphicsMBS.graphicsContextWithCGContextHandle(cg.Handle)

// and draw inside
ng.drawAtPoint "Hello World", new NSPointMBS(30, 30)
End Sub

Returns nil on any error.

See also:

Some examples using this method:

NSGraphicsMBS.graphicsContextWithCGContextHandle(targetCGContextRef as Ptr, initialFlippedState as boolean = false) as NSGraphicsMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method Cocoa Drawing MBS MacCocoa Plugin 20.0 ✅ Yes ❌ No ❌ No ✅ Yes All
Creates a new graphics context pointing to the given CGContext reference.
Example
Sub Paint(g As Graphics)
// get current context
dim cg as CGContextMBS = GetCurrentCGContextMBS

// get graphics context
dim ng as NSGraphicsMBS = NSGraphicsMBS.graphicsContextWithCGContextHandle(cg.Handle)

// and draw inside
ng.drawAtPoint "Hello World", new NSPointMBS(30, 30)
End Sub

Returns nil on any error.

See also:

NSGraphicsMBS.graphicsContextWithNSBitmapImageRep(targetImage as NSBitmapImageRepMBS) as NSGraphicsMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method Cocoa Drawing MBS MacCocoa Plugin 13.1 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Creates a new graphics context for drawing into a bitmap image representation.
Example
dim n as new NSImageMBS(300,300)
dim r as new NSBitmapImageRepMBS(300, 300, 8, 4, true, NSColorSpaceMBS.NSCalibratedRGBColorSpace, 4*300, 32)

dim g as NSGraphicsMBS = NSGraphicsMBS.graphicsContextWithNSBitmapImageRep(r)
g.SetColorRGB 1.0,0,0,0.5
g.fillRect 0, 0, 100, 100
g = nil // flush
n.addRepresentation r

Backdrop = n.CopyPictureWithMask

Please make sure the graphics object is destroyed (Set to nil) so the drawings flush to the image.
Returns nil on any error.

NSGraphicsMBS.graphicsContextWithNSImage(targetImage as NSImageMBS) as NSGraphicsMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method Cocoa Drawing MBS MacCocoa Plugin 13.1 ✅ Yes ❌ No ❌ No ✅ Yes All
Creates a new graphics context for drawing into an image.
Example
// make new image
dim myImage as new NSImageMBS(500,500)
dim myGraphics as NSGraphicsMBS = NSGraphicsMBS.graphicsContextWithNSImage(myImage)

// make logo image
dim myPicture as Picture = LogoMBS(500)
dim anotherImage as new NSImageMBS(myPicture)

// draw logo image to new image
myGraphics.drawInRect(anotherImage, 0, 0, myImage.width, myImage.height, 0, 0, anotherImage.width, anotherImage.height, myGraphics.NSCompositeSourceOver, 1.0)
myGraphics = nil // flush drawing

// save to file
dim f as FolderItem = SpecialFolder.Desktop.Child("test.jpg")
dim b as BinaryStream = BinaryStream.Create(f, True)
if b<>nil then
b.Write myImage.JPEGRepresentation
b.Close
end if

Please make sure the graphics object is destroyed (Set to nil) so the drawings flush to the image.
Returns nil on any error.

Some examples using this method:

NSGraphicsMBS.graphicsContextWithNSView(targetView as NSViewMBS) as NSGraphicsMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method Cocoa Drawing MBS MacCocoa Plugin 13.1 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Creates a new graphics context for drawing into a Cocoa view.
Example
// draws in a Cocoa view
dim gg as NSGraphicsMBS = NSGraphicsMBS.graphicsContextWithNSView(canvas1.NSViewMBS)
gg.SetColorRGB 1.0,0,0,0.5
gg.fillRect 0, 0, 100, 100
gg = nil // flush

Please make sure the graphics object is destroyed (Set to nil) so the drawings flush to the window.
Returns nil on any error.

NSGraphicsMBS.graphicsContextWithNSWindow(targetNSWindow as NSWindowMBS) as NSGraphicsMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method Cocoa Drawing MBS MacCocoa Plugin 13.1 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Creates a new graphics context for drawing into a window.
Example
// draw in a Cocoa window
dim g as NSGraphicsMBS = NSGraphicsMBS.graphicsContextWithNSWindow(window1.NSWindowMBS)
g.SetColorRGB 1.0,0,0,0.5
g.fillRect 0, 0, 100, 100
g = nil // flush

Returns nil on any error.

NSGraphicsMBS.graphicsContextWithWindow(targetWindow as DesktopWindow) as NSGraphicsMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method Cocoa Drawing MBS MacCocoa Plugin 22.0 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Creates a new graphics context for drawing into a window.

Returns nil on any error.

See also:

NSGraphicsMBS.graphicsContextWithWindow(targetWindow as window) as NSGraphicsMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method Cocoa Drawing MBS MacCocoa Plugin 13.1 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Creates a new graphics context for drawing into a window.
Example
// draws in a Cocoa window
dim g as NSGraphicsMBS = NSGraphicsMBS.graphicsContextWithWindow(window1)
g.SetColorRGB 1.0,0,0,0.5
g.fillRect 0, 0, 100, 100
g = nil // flush

Returns nil on any error.

See also:

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


The biggest plugin in space...