Platforms to show: All Mac Windows Linux Cross-Platform

Back to CGBitmapContextMBS class.

CGBitmapContextMBS.Create(data as memoryblock, width as Integer, height as Integer, bitsPerComponent as Integer, bytesPerRow as Integer, colorspace as CGColorSpaceMBS, alphaInfo as Integer) as CGBitmapContextMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method CoreGraphics MBS MacCG Plugin 13.1 ✅ Yes ❌ No ❌ No ✅ Yes All
Create a bitmap context.

The context draws into a bitmap which is 'width' pixels wide and 'height' pixels high. The number of components for each pixel is specified by 'colorspace', which also may specify a destination color profile. The number of bits for each component of a pixel is specified by 'bitsPerComponent', which must be 1, 2, 4, or 8. Each row of the bitmap consists of 'bytesPerRow' bytes, which must be at least '(width * bitsPerComponent * number of components + 7)/8' bytes. 'data' points a block of memory at least 'bytesPerRow * height' bytes. 'alphaInfo' specifies whether the bitmap should contain an alpha channel, and how it's to be generated.

Fails if data=nil or colorspace=nil.
The memoryblock is not referenced and not stored, so keep it alive while using the BitmapContext object.

Returns nil on any error.

data
A pointer to the destination in memory where the drawing is to be rendered. The size of this memoryblock should be at least(bytesPerRow*height) bytes. Can be nil to let system allocate it.

width
The width of the bitmap in pixels.

height
The height of the bitmap in pixels.

bitsPerComponent
The number of bits to use for each component of a pixel in memory. Allowable values are 4, 5, or 8. For example, for a 32-bit RGB(A) colorspace, you would specify a value of 8 bits per color component. In combination, the number of bits per component, the color space, and the alpha value determine which bitmap context formats Quartz supports.

bytesPerRow
The number of bytes of memory to use per row of the bitmap. This value must be at least the product of the width and bitsPerComponentparameters, times the number of components per pixel. The result should be divided by 8 and rounded up to the nearest whole number to obtain the number of bytes to use per row. That is, the value must be at least (((width)*(bits per component)*(number of components per pixel))+7)/8 bytes. For a given row, Quartz stores bitmap data for the first width pixels and ignores any remaining bytes. The colorspace value referenced by the colorspace parameter specifies the number of components for each pixel. Can be zero to let system decide (especially if data is nil)

colorspace
The color space to use for the bitmap context.

alphaInfo
A CGImageAlphaInfo constant specifying whether the bitmap should contain an alpha channel and how it is to be generated. The alpha value determines the opacity of a pixel when it is drawn.

Supported pixel formats:

Pixel formatColor spaceBits per pixelBits per componentAlpha option
Gray_8Grayscale88kCGImageAlphaNone
RGB555RGB165kCGImageAlphaNoneSkipFirst
XRGB_32RGB328kCGImageAlphaNoneSkipFirst
ARGB_32RGB328kCGImageAlphaPremultipliedFirst
RGBX_32RGB328kCGImageAlphaNoneSkipLast
RGBA_32RGB328kCGImageAlphaPremultipliedLast

Quartz does not support the following formats in a bitmap context:

  • 1-bit grayscale
  • 24-bit RGB
  • CMYK (any depth)

CGImageAlphaInfo constants:
kCGImageAlphaNone0
kCGImageAlphaPremultipliedLast1For example, premultiplied RGBA
kCGImageAlphaPremultipliedFirst2For example, premultiplied ARGB
kCGImageAlphaLast3For example, non-premultiplied RGBA
kCGImageAlphaFirst4For example, non-premultiplied ARGB
kCGImageAlphaNoneSkipLast5Equivalent to kCGImageAlphaNone.
kCGImageAlphaNoneSkipFirst6

See also:

CGBitmapContextMBS.Create(Other as CGBitmapContextMBS, NewColorspace as CGColorSpaceMBS) as CGBitmapContextMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method CoreGraphics MBS MacCG Plugin 14.3 ✅ Yes ❌ No ❌ No ✅ Yes All
Creates a new bitmap object.
Example
dim pic as new Picture(100,100) // some picture
dim ICCProfileData as memoryblock // get a IIC Profile somewhere

dim colorspace as CGColorSpaceMBS = CGColorSpaceMBS.CreateWithICCProfile(ICCProfileData)
dim bitmap as CGBitmapContextMBS = CGBitmapContextMBS.CreateWithPicture(pic)
dim zweiteBitmap as CGBitmapContextMBS = bitmap.Create(bitmap, colorspace)

The new bitmap object uses same data as existing object, just accesses the pixels using the new color space.

See also:

CGBitmapContextMBS.CreateRGB(data as memoryblock, width as Integer, height as Integer, bytesPerRow as Integer, colorspace as CGColorSpaceMBS = nil) as CGBitmapContextMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method CoreGraphics MBS MacCG Plugin 15.0 ✅ Yes ❌ No ❌ No ✅ Yes All
Convenience function to handle RGB data.

Same as Create method, but for RGB data.
Converts data from 3 byte/pixel to 4 byte/pixel and than creates CGBitmapContextMBS.
Colorspace is optional and defaults to Generic RGB.
Returns nil on error, raises OutOfBounds exception for invalid parameters.

Some examples using this method:

CGBitmapContextMBS.CreateWithPicture(Pic as Picture) as CGBitmapContextMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method CoreGraphics MBS MacCG Plugin 13.1 ✅ Yes ❌ No ❌ No ✅ Yes All
Creates a CGBitmapContext referencing the given picture.
Example
// a new picture in RB
dim pic as new Picture(500, 500)

// and create CGBitmapContextMBS pointing to it
dim b as CGBitmapContextMBS = CGBitmapContextMBS.CreateWithPicture(pic)

// color set to full red
b.SetRGBFillColor 1.0, 0.0, 0.0, 1.0

// draw ellipse
dim r as CGRectMBS = CGRectMBS.Make(0, 0, 500, 500)
b.FillEllipseInRect r

// flush drawings
b.Flush

// and show
Backdrop = pic

Only for Cocoa target.
The plugin will do a clear cache on the picture in the destructor.

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


The biggest plugin in space...