Platforms to show: All Mac Windows Linux Cross-Platform

Back to NSImageMBS class.

NSImageMBS.addRepresentation(img as NSImageRepMBS)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 8.4 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Adds the specified image representation object to to the receiver.

After invoking this method, you may need to explicitly set features of the new image representation, such as the size, number of colors, and so on. This is true particularly when the NSImage object has multiple image representations to choose from. See NSImageRep and its subclasses for the methods you use to complete initialization.

Any representation added by this method is retained by the receiver. Image representations cannot be shared among multiple NSImage objects.

Some examples using this method:

NSImageMBS.BMPRepresentation as Memoryblock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ✅ Yes All
The image as the binary data in a BMP file.
Example
Var img as NSImageMBS
Var p as Picture
Var f as FolderItem
Var b as BinaryStream

p=New Picture(100,100,32)
p.Graphics.ForeColor=&cFF0000
p.Graphics.FillOval 0,0,100,100
Backdrop=p
img=new NSImageMBS(p,p.Mask)

f=SpecialFolder.Desktop.Child("test.bmp")
b=f.CreateBinaryFile("")
b.Write img.BMPRepresentation
b.Close

f.Launch

BMP does not support masks.

NSImageMBS.BMPRepresentationMT as Memoryblock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 13.1 ✅ Yes ❌ No ❌ No ✅ Yes All
The image as the binary data in a BMP file.

BMP does not support masks.

The work is performed on a preemptive thread, so this function does not block the application and can yield time to other Xojo threads. Must be called in a Xojo thread to enjoy benefits. If called in main thread will block, but keep other background threads running.
If you run several threads calling MT methods, you can get all CPU cores busy while main thread shows GUI with progress window.

NSImageMBS.DrawIntoCGContextAtPoint(cgcontext as Integer, x as Double, y as Double, sx as Double, sy as Double, SourceW as Double, SourceH as Double, operation as Integer, fraction as Double) as boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ✅ Yes All
Draws the image.

Same as DrawIntoCGContextAtRect, but with a point instead of a rectangle.

NSImageMBS.DrawIntoCGContextAtRect(cgcontext as Integer, x as Double, y as Double, w as Double, h as Double, SourceX as Double, SourceY as Double, SourceW as Double, SourceH as Double, operation as Integer, fraction as Double) as boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ✅ Yes All
Draws the image.
Example
Function OpenAsNSimage(extends file as folderitem) As picture
Var width as Integer
Var height as Integer
Var c as NSImageMBS
Var g as CGPictureContextMBS

// load image from file
c=new NSImageMBS

// is that image valid?
if c.initWithContentsOfURL(file) then
Width=c.Width
height=c.Height

// create a drawing buffer to draw inside
g=new CGPictureContextMBS(width,height)

if g.Handle<>0 then // valid?

// now draw the image inside.
// you could scale or even apply transparency...
if c.DrawIntoCGContextAtRect(g.Handle, 0, 0, width, height, 0,0,width,height,2,1.0) then
// make a RB Picture from it
Return g.CopyPicture
end if
end if
end if
End Function

Draws the image into a CGContext. You need to specify first the destination rectangle followed by the source rectangle.

fraction:
The opacity of the image, specified as a value from 0.0 to 1.0. Specifying a value of 0.0 draws the image as fully transparent while a value of 1.0 draws the image as fully opaque. Values greater than 1.0 are interpreted as 1.0.

operation codes:
NSCompositeClear = 0Transparent.
NSCompositeCopy = 1Source image.
NSCompositeSourceOver = 2Source image wherever source image is opaque, and destination image elsewhere.
NSCompositeSourceIn = 3Source image wherever both images are opaque, and transparent elsewhere.
NSCompositeSourceOut = 4Source image wherever source image is opaque but destination image is transparent, and transparent elsewhere.
NSCompositeSourceAtop = 5Source image wherever both images are opaque, destination image wherever destination image is opaque but source image is transparent, and transparent elsewhere.
NSCompositeDestinationOver = 6Destination image wherever destination image is opaque, and source image elsewhere.
NSCompositeDestinationIn = 7Destination image wherever both images are opaque, and transparent elsewhere.
NSCompositeDestinationOut = 8Destination image wherever destination image is opaque but source image is transparent, and transparent elsewhere.
NSCompositeDestinationAtop = 9Destination image wherever both images are opaque, source image wherever source image is opaque but destination image is transparent, and transparent elsewhere.
NSCompositeXOR = 10Exclusive OR of source and destination images.
NSCompositePlusDarker = 11Sum of source and destination images, with color values approaching 0 as a limit.
NSCompositeHighlight = 12Source image wherever source image is opaque, and destination image elsewhere.
NSCompositePlusLighter = 13Sum of source and destination images, with color values approaching 1 as a limit.

Returns true on success and false on failure.

Some examples using this method:

NSImageMBS.GIFRepresentation as Memoryblock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ✅ Yes All
The image as the binary data in a GIF file.
Example
Var img as NSImageMBS
Var p as Picture
Var f as FolderItem
Var b as BinaryStream

p=New Picture(100,100,32)
p.Graphics.ForeColor=&cFF0000
p.Graphics.FillOval 0,0,100,100
Backdrop=p
img=new NSImageMBS(p,p.Mask)

f=SpecialFolder.Desktop.Child("test.gif")
b=f.CreateBinaryFile("")
b.Write img.GIFRepresentation
b.Close

f.Launch

GIF does support masks in a limited way.

NSImageMBS.GIFRepresentationMT as Memoryblock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 13.1 ✅ Yes ❌ No ❌ No ✅ Yes All
The image as the binary data in a GIF file.

GIF does support masks in a limited way.

The work is performed on a preemptive thread, so this function does not block the application and can yield time to other Xojo threads. Must be called in a Xojo thread to enjoy benefits. If called in main thread will block, but keep other background threads running.
If you run several threads calling MT methods, you can get all CPU cores busy while main thread shows GUI with progress window.

NSImageMBS.JPEGRepresentation as Memoryblock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ✅ Yes All
The image as the binary data in a JPEG file.
Example
Var img as NSImageMBS
Var p as Picture
Var f as FolderItem
Var b as BinaryStream

p=New Picture(100,100,32)
p.Graphics.ForeColor=&cFF0000
p.Graphics.FillOval 0,0,100,100
Backdrop=p
img=new NSImageMBS(p)

f=SpecialFolder.Desktop.Child("test.jpeg")
b=f.CreateBinaryFile("")
b.Write img.JPEGRepresentation
b.Close

f.Launch

JPEG does not support masks. Uses 80% for the quality.

NSImageMBS.JPEGRepresentationMT as Memoryblock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 13.1 ✅ Yes ❌ No ❌ No ✅ Yes All
The image as the binary data in a JPEG file.
Example
// take some Picture
Var logo as Picture = LogoMBS(500)

// make a NSImageMBS from it
Var nsimage as new NSImageMBS(logo)

// use thread friendly compress function
Var jpeg as MemoryBlock = nsimage.JPEGRepresentationMT

// decode to see if it worked
Var test as Picture = JPEGStringToPictureMBS(jpeg)

// and display
Backdrop = test

JPEG does not support masks. Uses 80% for the quality.

The work is performed on a preemptive thread, so this function does not block the application and can yield time to other Xojo threads. Must be called in a Xojo thread to enjoy benefits. If called in main thread will block, but keep other background threads running.
If you run several threads calling MT methods, you can get all CPU cores busy while main thread shows GUI with progress window.

NSImageMBS.JPEGRepresentationWithCompressionFactor(factor as Double) as Memoryblock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ✅ Yes All
The image as the binary data in a JPEG file.
Example
Var img as NSImageMBS
Var p as Picture
Var f as FolderItem
Var b as BinaryStream

p=New Picture(100,100,32)
p.Graphics.ForeColor=&cFF0000
p.Graphics.FillOval 0,0,100,100
Backdrop=p
img=new NSImageMBS(p)

f=SpecialFolder.Desktop.Child("test.jpeg")
b=f.CreateBinaryFile("")
b.Write img.JPEGRepresentationWithCompressionFactor(0.01)
b.Close

f.Launch

Factor for compression goes from 0.0 to 1.0.
JPEG does not support masks.

Some examples using this method:

NSImageMBS.JPEGRepresentationWithCompressionFactorMT(factor as Double) as Memoryblock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 13.1 ✅ Yes ❌ No ❌ No ✅ Yes All
The image as the binary data in a JPEG file.
Example
// take some Picture
Var logo as Picture = LogoMBS(500)

// make a NSImageMBS from it
Var nsimage as new NSImageMBS(logo)

// use thread friendly compress function
Var jpeg as MemoryBlock = nsimage.JPEGRepresentationWithCompressionFactorMT(1.0)

// decode to see if it worked
Var test as Picture = JPEGStringToPictureMBS(jpeg)

// and display
Backdrop = test

Factor for compression goes from 0.0 to 1.0.
JPEG does not support masks.

The work is performed on a preemptive thread, so this function does not block the application and can yield time to other Xojo threads. Must be called in a Xojo thread to enjoy benefits. If called in main thread will block, but keep other background threads running.
If you run several threads calling MT methods, you can get all CPU cores busy while main thread shows GUI with progress window.

Some examples using this method:

NSImageMBS.PNGRepresentation as Memoryblock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ✅ Yes All
The image as the binary data in a PNG file.
Example
Var img as NSImageMBS
Var p as Picture
Var f as FolderItem
Var b as BinaryStream

p=New Picture(100,100,32)
p.Graphics.ForeColor=&cFF0000
p.Graphics.FillOval 0,0,100,100
Backdrop=p
img=new NSImageMBS(p,p.Mask)

f=SpecialFolder.Desktop.Child("test.png")
b=f.CreateBinaryFile("")
b.Write img.PNGRepresentation
b.Close

f.Launch

PNG does support masks.

NSImageMBS.PNGRepresentationMT as Memoryblock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 13.1 ✅ Yes ❌ No ❌ No ✅ Yes All
The image as the binary data in a PNG file.
Example
// take some Picture
Var logo as Picture = LogoMBS(500)

// make a NSImageMBS from it
Var nsimage as new NSImageMBS(logo)

// use thread friendly compress function
Var jpeg as MemoryBlock = nsimage.PNGRepresentationMT

// decode to see if it worked
Var test as Picture = PNGStringToPictureMBS(jpeg)

// and display
Backdrop = test

PNG does support masks.

The work is performed on a preemptive thread, so this function does not block the application and can yield time to other Xojo threads. Must be called in a Xojo thread to enjoy benefits. If called in main thread will block, but keep other background threads running.
If you run several threads calling MT methods, you can get all CPU cores busy while main thread shows GUI with progress window.

NSImageMBS.recache

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 9.6 ✅ Yes ❌ No ❌ No ✅ Yes All
Invalidates and frees the offscreen caches of all image representations.

If you modify an image representation, you must send a recache message to the corresponding image object to force the changes to be recached. The next time any image representation is drawn, it is asked to recreate its cached image. If you do not send this message, the image representation may use the old cache data. This method simply clears the cached image data; it does not delete the NSCachedImageRep objects associated with any image representations.
If you do not plan to use an image again right away, you can free its caches to reduce the amount of memory consumed by your program.

NSImageMBS.removeRepresentation(img as NSImageRepMBS)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 8.4 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Removes the specified image representation from the receiver and releases it.

Some examples using this method:

NSImageMBS.RepresentationHeight(index as Integer) as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Height of the representation with the given index in pixel.

Index from 0 to RepresetationCount-1.
Returns 0 on invalid index.

NSImageMBS.representations as NSImageRepMBS()

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Returns an array containing all of the receiver's image representations.

An array containing zero or more NSImageRep objects.
Returns nil on any error.

Some examples using this method:

NSImageMBS.RepresentationWidth(index as Integer) as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Width of the representation with the given index in pixel.

Index from 0 to RepresetationCount-1.
Returns 0 on invalid index.

NSImageMBS.setName(value as String) as Boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Registers the image under the specified name.

Returns true if the receiver was successfully registered with the given name; otherwise, false.

Discussion
If the receiver is already registered under a different name, this method unregisters the other name. If a different image is registered under the name specified in aString, this method does nothing and returns false.

When naming an image using this method, it is convention not to include filename extensions in the names you specify. That way, you can easily distinguish between images you have named explicitly and those you want to load from the application's bundle. For information about the rules used to search for images, see the imageNamed method.

NSImageMBS.setSize(width as Double, height as Double)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Sets the width and height of the image.

Pass the new size of the image, measured in points.

Discussion:
The size of an NSImage object must be set before it can be used. If the size of the image hasn't already been set when an image representation is added, the size is taken from the image representation's data. For EPS images, the size is taken from the image's bounding box. For TIFF images, the size is taken from the ImageLength and ImageWidth attributes.

Changing the size of an NSImage after it has been used effectively resizes the image. Changing the size invalidates all its caches and frees them. When the image is next composited, the selected representation will draw itself in an offscreen window to recreate the cache.

NSImageMBS.TIFFRepresentation as Memoryblock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ✅ Yes All
Returns a string containing TIFF data for all of the image representations in the receiver.
Example
Var p As Picture = LogoMBS(500)

// save as tiff

Var n As New NSImageMBS(p)
Var tiff As String = n.TIFFRepresentation
Var file1 As FolderItem = SpecialFolder.Desktop.Child("test1.tif")
Var b As BinaryStream = BinaryStream.Create(file1)
b.Write tiff
b.Close

A string containing the TIFF data, or nil if the TIFF data could not be created.

Discussion:
You can use the returned data object to write the TIFF data to a file. For each image representation, this method uses the TIFF compression option associated with that representation or NSTIFFCompressionNone, if no option is set.

If one of the receiver's image representations does not support the creation of TIFF data natively (PDF and EPS images, for example), this method creates the TIFF data from that representation's cached content.

NSImageMBS.TIFFRepresentationMT as Memoryblock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 13.1 ✅ Yes ❌ No ❌ No ✅ Yes All
Returns a string containing TIFF data for all of the image representations in the receiver.
Example
// take some Picture
Var logo as Picture = LogoMBS(500)

// make a NSImageMBS from it
Var nsimage as new NSImageMBS(logo)

// use thread friendly compress function
Var data as MemoryBlock = nsimage.TIFFRepresentationMT

// decode to see if it worked
Var test as Picture = TIFFStringToPictureMBS(data)

// and display
Backdrop = test

A string containing the TIFF data, or nil if the TIFF data could not be created.

Discussion:
You can use the returned data object to write the TIFF data to a file. For each image representation, this method uses the TIFF compression option associated with that representation or NSTIFFCompressionNone, if no option is set.

If one of the receiver's image representations does not support the creation of TIFF data natively (PDF and EPS images, for example), this method creates the TIFF data from that representation's cached content.

The work is performed on a preemptive thread, so this function does not block the application and can yield time to other Xojo threads. Must be called in a Xojo thread to enjoy benefits. If called in main thread will block, but keep other background threads running.
If you run several threads calling MT methods, you can get all CPU cores busy while main thread shows GUI with progress window.

NSImageMBS.TIFFRepresentationUsingCompression(comp as Integer, factor as Double) as Memoryblock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 7.2 ✅ Yes ❌ No ❌ No ✅ Yes All
Returns a string containing TIFF data for all of the image representations in the receiver.

comp: The type of compression to use. For a list of values, see the constants in NSBitmapImageRep.

aFloat: Provides a hint for compression types that implement variable compression ratios. Currently, only JPEG compression uses a compression factor.

Returns a string containing the TIFF data, or nil if the TIFF data could not be created.

Discussion:
You can use the returned data object to write the TIFF data to a file. If the specified compression isn't applicable, no compression is used. If a problem is encountered during generation of the TIFF data, this method may raise an exception.

If one of the receiver's image representations does not support the creation of TIFF data natively (PDF and EPS images, for example), this method creates the TIFF data from that representation's cached content.

NSImageMBS.TIFFRepresentationUsingCompressionMT(comp as Integer, factor as Double) as Memoryblock

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Cocoa Drawing MBS MacBase Plugin 13.1 ✅ Yes ❌ No ❌ No ✅ Yes All
Returns a string containing TIFF data for all of the image representations in the receiver.

comp: The type of compression to use. For a list of values, see the constants in NSBitmapImageRep.

aFloat: Provides a hint for compression types that implement variable compression ratios. Currently, only JPEG compression uses a compression factor.

Returns a string containing the TIFF data, or nil if the TIFF data could not be created.

Discussion:
You can use the returned data object to write the TIFF data to a file. If the specified compression isn't applicable, no compression is used. If a problem is encountered during generation of the TIFF data, this method may raise an exception.

If one of the receiver's image representations does not support the creation of TIFF data natively (PDF and EPS images, for example), this method creates the TIFF data from that representation's cached content.

The work is performed on a preemptive thread, so this function does not block the application and can yield time to other Xojo threads. Must be called in a Xojo thread to enjoy benefits. If called in main thread will block, but keep other background threads running.
If you run several threads calling MT methods, you can get all CPU cores busy while main thread shows GUI with progress window.

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


The biggest plugin in space...