GMImage.SetBackgroundColor
--------------------------

Sets the Image background color.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [GraphicsMagick](component_GraphicsMagick.md) | [2.0](newinversion20.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "GMImage.SetBackgroundColor"; ImageRef; Color ) 

**MBS**( **"GMImage.SetBackgroundColor";** /\* Sets the Image background color. \*/  
**$ImageRef**; /\* The image reference number.e.g. 1 \*/   
**$Color**) /\* A color value.   
This can be a string in the following format "HSL h s l a", "YUV y u v a", "RGB r g b a", "MONO m a", "GRAY g a" or "COLOR R G B a". Alpha value a is optional. Range of values is 0 to 1.0 except if you use COLOR where RGB values are 0 to 65535.  
Starting with version 5.4 of our plugin, you can use hex notation. # followed by 2 digits for red, green, blue and optional alpha, e.g. #FF0000 for full red.e.g. "RGB 1 0 0" \*/ 

### Parameters

| Parameter | Description | Example |
|---|---|---|
| ImageRef | The image reference number. | 1 |
| Color | A color value.    This can be a string in the following format "HSL h s l a", "YUV y u v a", "RGB r g b a", "MONO m a", "GRAY g a" or "COLOR R G B a". Alpha value a is optional. Range of values is 0 to 1.0 except if you use COLOR where RGB values are 0 to 65535.   Starting with version 5.4 of our plugin, you can use hex notation. # followed by 2 digits for red, green, blue and optional alpha, e.g. #FF0000 for full red. | "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](GMImageNew.md)"; "16x16"; "white")\]  
\#Add alpha channel   
Set Variable \[$r ; Value:MBS ( "[GMImage.SetMatte](GMImageSetMatte.md)"; $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](GMImageErase.md)"; $img )\]  
\#Write to field   
Set Variable \[$pic ; Value:MBS ( "[GMImage.WriteToPNGContainer](GMImageWriteToPNGContainer.md)"; $img ; "test.png" )\]  
Set Field \[test::r ; $pic \]  
\#Cleanup   
Set Variable \[$r ; Value:MBS ( "[GMImage.Release](GMImageRelease.md)"; $img )\]  
Convert a SVG image to PNG with alpha channel:

 # Load image from container   
Set Variable \[ $image ; Value: MBS ( "[GMImage.New](GMImageNew.md)"; "500x500"; "transparent" ) \]   
\# turn on alpha channel and set background to transparent   
Set Variable \[ $r ; Value: MBS ( "[GMImage.SetType](GMImageSetType.md)"; $image ; 7 ) \]   
Set Variable \[ $r ; Value: MBS ( "GMImage.SetBackgroundColor"; $Image ; "transparent" ) \]   
\# read SVG   
Set Variable \[ $r ; Value: MBS ( "[GMImage.ReadContainer](GMImageReadContainer.md)"; $image ; SVG to PNG::InputFile ; "500x500"; 0; "SVG" ) \]   
\# Output to container and release image   
Set Field \[ SVG to PNG::OutputFile ; MBS ( "[GMImage.WriteToPNGContainer](GMImageWriteToPNGContainer.md)"; $image ; "test.png") \]   
Set Variable \[ $r ; Value: MBS ("[GMImage.Free](GMImageFree.md)"; $image ) \]  
Create a transparent picture by erasing with transparent color:

 Set Variable \[ $img ; Value: MBS ("[GMImage.New](GMImageNew.md)"; $width &amp; "x" &amp; $height ; "RGB 1 1 1") \]   
Set Variable \[ $r ; Value: MBS ( "[GMImage.SetMatte](GMImageSetMatte.md)"; $img ; 1) \]   
Set Variable \[ $r ; Value: MBS ( "GMImage.SetBackgroundColor"; $img ; "transparent") \]   
Set Variable \[ $r ; Value: MBS ("[GMImage.Erase](GMImageErase.md)"; $img ) \]  
Removes alpha channel:

 # Load from container   
Set Variable \[ $BigImageRef ; Value: MBS ("[GMImage.NewFromContainer](GMImageNewFromContainer.md)"; 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](GMImageFlatten.md)"; $BigImageRef ) \]  
\# Store output and release the image   
Set Field \[ Test::Result ; MBS ( "[GMImage.WriteToJPEGContainer](GMImageWriteToJPEGContainer.md)"; $BigImageRef ) \]  
Set Variable \[ $Error ; Value: MBS ("[GMImage.Free](GMImageFree.md)";$BigImageRef ) \]  
### See also

- [GMImage.GetBackgroundColor](GMImageGetBackgroundColor.md)
- [GMImage.GetBackgroundTexture](GMImageGetBackgroundTexture.md)
- [GMImage.New](GMImageNew.md)
- [GMImage.NewFromContainer](GMImageNewFromContainer.md)
- [GMImage.ReadContainer](GMImageReadContainer.md)
- [GMImage.ReplaceColor](GMImageReplaceColor.md)
- [GMImage.SetColorFuzz](GMImageSetColorFuzz.md)
- [GMImage.Trim](GMImageTrim.md)
- [GMImage.WriteToJPEGContainer](GMImageWriteToJPEGContainer.md)
- [GMImage.WriteToPNGContainer](GMImageWriteToPNGContainer.md)

### Example Databases

- [GraphicsMagick/SVG to PNG](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/GraphicsMagick/SVG%20to%20PNG.shtml#1ScriptAnchor_)
- [GraphicsMagick/Trim Picture](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/GraphicsMagick/Trim%20Picture.shtml#1ScriptAnchor_)
- [Overlay](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Overlay.shtml#4ScriptAnchor_)

### Blog Entries

- [Convert a SVG image to PNG with alpha channel](https://www.mbsplugins.de/archive/2020-04-07/Convert_a_SVG_image_to_PNG_wit/monkeybreadsoftware_blog_filemaker)

This function checks for a license.

Created 18th August 2014 , last changed 25th February 2026

  
[GMImage.SetAttribute](GMImageSetAttribute.md) - [GMImage.SetBackgroundTexture](GMImageSetBackgroundTexture.md)

[HTML Version](GMImageSetBackgroundColor.shtml)