运行上述代码后会在项目的文件夹里生产一个hello.png文件
以下是实际使用时的代码示例:
//全局变量
cairo_surface_t *surface;
cairo_t *cr;
//帮助函数:
//InitCairo用于初始化画图设置,例如画笔宽度,字体大小,背景颜色
void InitCairo(double canvasWidth)
{
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, canvasWidth, canvasWidth);
cr = cairo_create (surface);
//line
cairo_set_line_width(cr, 2);
//font
cairo_select_font_face (cr, "serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, 18.0);
//white background
cairo_rectangle (cr, 0,0,canvasWidth, canvasWidth);
cairo_set_source_rgb (cr, 255, 255, 255);
cairo_fill (cr);
}
//CompleteCairo函数用于保存画图,回收资源
void CompleteCairo(const char* fileName)
{
cairo_destroy (cr);
cairo_surface_write_to_png (surface, fileName);
cairo_surface_destroy (surface);
}
//示例
int main()
{
InitCairo (800);
cairo_move_to(cr, 0,0);
cairo_line_to(cr, 50,50);
cairo_set_source_rgb(cr, 0,0,0);
cairo_stroke(cr);
CompleteCairo ("test.png");
}