Xojo Developer Conference
25/27th April 2018 in Denver.
MBS Xojo Conference
6/7th September 2018 in Munich, Germany.

Platforms to show: All Mac Windows Linux Cross-Platform

NSGraphicsMBS.graphicsContext as NSGraphicsMBS
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
shared method Cocoa Drawing MBS MacCocoa Plugin 13.1 Yes No No Yes, macOS only No
Function: 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
Notes: Returns nil on any error.

Feedback, Comments & Corrections

NSGraphicsMBS.graphicsContextWithCGContext(targetCGContext as Variant, initialFlippedState as boolean = false) as NSGraphicsMBS
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
shared method Cocoa Drawing MBS MacCocoa Plugin 13.1 Yes No No Yes, macOS only No
Function: 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
Notes: Returns nil on any error.

Feedback, Comments & Corrections

NSGraphicsMBS.graphicsContextWithCGContextHandle(targetCGContextRef as Integer, initialFlippedState as boolean = false) as NSGraphicsMBS
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
shared method Cocoa Drawing MBS MacCocoa Plugin 15.0 Yes No No Yes, macOS only No
Function: 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
Notes: Returns nil on any error.

Feedback, Comments & Corrections

NSGraphicsMBS.graphicsContextWithNSBitmapImageRep(targetImage as NSBitmapImageRepMBS) as NSGraphicsMBS
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
shared method Cocoa Drawing MBS MacCocoa Plugin 13.1 Yes No No Yes, macOS only No
Function: 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
Notes:
Please make sure the graphics object is destroyed (Set to nil) so the drawings flush to the image.
Returns nil on any error.

Feedback, Comments & Corrections

NSGraphicsMBS.graphicsContextWithNSImage(targetImage as NSImageMBS) as NSGraphicsMBS
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
shared method Cocoa Drawing MBS MacCocoa Plugin 13.1 Yes No No Yes, macOS only No
Function: 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
Notes:
Please make sure the graphics object is destroyed (Set to nil) so the drawings flush to the image.
Returns nil on any error.

Feedback, Comments & Corrections

NSGraphicsMBS.graphicsContextWithNSView(targetView as NSViewMBS) as NSGraphicsMBS
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
shared method Cocoa Drawing MBS MacCocoa Plugin 13.1 Yes No No Yes, macOS only No
Function: 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
Notes:
Please make sure the graphics object is destroyed (Set to nil) so the drawings flush to the window.
Returns nil on any error.

Feedback, Comments & Corrections

NSGraphicsMBS.graphicsContextWithNSWindow(targetNSWindow as NSWindowMBS) as NSGraphicsMBS
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
shared method Cocoa Drawing MBS MacCocoa Plugin 13.1 Yes No No Yes, macOS only No
Function: 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
Notes: Returns nil on any error.

Feedback, Comments & Corrections

NSGraphicsMBS.graphicsContextWithWindow(targetWindow as window) as NSGraphicsMBS
Type Topic Plugin Version macOS Windows Linux Console & Web iOS
shared method Cocoa Drawing MBS MacCocoa Plugin 13.1 Yes No No No No
Function: 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
Notes: Returns nil on any error.

Feedback, Comments & Corrections

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




Links
MBS Xojo Plugins