Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

GMImage.SetBackgroundColor

Sets the Image background color.

Component Version macOS Windows Linux Server iOS SDK
GraphicsMagick 2.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "GMImage.SetBackgroundColor"; ImageRef; Color )   More

Parameters

Parameter Description Example
ImageRef The image reference number. 1
Color A color specification.
Multiple formats are supported including various color names and # with hex number.
See FAQ for details.
"RGB 1 0 0"

Result

Returns "OK" on success.

Description

Sets the Image background color.

Examples

Create picture with red and 50% alpha:

#Create picture
Set Variable [$img; Value:MBS( "GMImage.New"; "16x16"; "white")]
#Add alpha channel
Set Variable [$r; Value:MBS( "GMImage.SetMatte"; $img; 1)]
#Set background color and fill
Set Variable [$r; Value:MBS( "GMImage.SetBackgroundColor"; $img; "COLOR 65535 0 0 0.5")]
Set Variable [$r; Value:MBS( "GMImage.Erase"; $img)]
#Write to field
Set Variable [$pic; Value:MBS( "GMImage.WriteToPNGContainer"; $img; "test.png" )]
Set Field [test::r; $pic]
#Cleanup
Set Variable [$r; Value:MBS( "GMImage.Release"; $img)]

Convert a SVG image to PNG with alpha channel:

# Load image from container
Set Variable [ $image ; Value: MBS( "GMImage.New"; "500x500"; "transparent" ) ]
# turn on alpha channel and set background to transparent
Set Variable [ $r ; Value: MBS( "GMImage.SetType"; $image; 7 ) ]
Set Variable [ $r ; Value: MBS( "GMImage.SetBackgroundColor"; $Image; "transparent" ) ]
# read SVG
Set Variable [ $r ; Value: MBS( "GMImage.ReadContainer"; $image; SVG to PNG::InputFile; "500x500"; 0; "SVG" ) ]
# Output to container and release image
Set Field [ SVG to PNG::OutputFile ; MBS( "GMImage.WriteToPNGContainer"; $image; "test.png") ]
Set Variable [ $r ; Value: MBS("GMImage.Free"; $image) ]

Create a transparent picture by erasing with transparent color:

Set Variable [ $img ; Value: MBS("GMImage.New"; $width & "x" & $height; "RGB 1 1 1") ]
Set Variable [ $r ; Value: MBS( "GMImage.SetMatte"; $img; 1) ]
Set Variable [ $r ; Value: MBS( "GMImage.SetBackgroundColor"; $img; "transparent") ]
Set Variable [ $r ; Value: MBS("GMImage.Erase"; $img) ]

Removes alpha channel:

# Load from container
Set Variable [ $BigImageRef ; Value: MBS("GMImage.NewFromContainer"; Test::BigImage) ]
# set the color to use for transparent areas
Set Variable [ $Result ; Value: MBS("GMImage.SetBackgroundColor"; $BigImageRef; "white") ]
# and remove transparency
Set Variable [ $Result ; Value: MBS("GMImage.Flatten"; $BigImageRef) ]
# Store output and release the image
Set Field [ Test::Result ; MBS( "GMImage.WriteToJPEGContainer"; $BigImageRef ) ]
Set Variable [ $Error ; Value: MBS("GMImage.Free";$BigImageRef) ]

See also

Example Databases

Blog Entries

This function checks for a license.

Created 18th August 2014, last changed 30th June 2026


GMImage.SetAttribute - GMImage.SetBackgroundTexture


MarkDown version: GMImageSetBackgroundColor.md