Perl Cairo tutorial - Fill in the background

This is one page of a Perl Cairo tutorial.

This example shows checking if PNG is there, setting a drawing colour with set_source_rgb, drawing a rectangle with rectangle, filling the rectangle with fill, and writing the rectangle to a PNG file with write_to_png.
use warnings;
use strict;
use Cairo;
# Check we can use PNGs
if (! Cairo::HAS_PNG_FUNCTIONS) {
    die "png backend not supported";
# Create a drawing surface.
my $surface = Cairo::ImageSurface->create ('argb32', 100, 100);
# Create a drawing context.
my $cr = Cairo::Context->create ($surface);
# Create a rectangle
$cr->rectangle (0, 0, 100, 100);
# Set the colours to red=0, green=0, blue=0.
$cr->set_source_rgb (0, 0, 0);
# Fill the rectangle.
$cr->fill ();
# The name of the output file.
my $png = 'simple-image.png';
# Write the surface to the png file specified by $png.
$surface->write_to_png ($png);


Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock ( or use the discussion group at Google Groups. / Privacy / Disclaimer