Cairo Library:生成一个白色背景的png文件

时间:2018-01-18 14:40:00

标签: c png cairo

我在INTERNET中搜索了与该主题相关的内容,但我发现只有那些想要保存背景透明的png文件的人。

我报告了我现在使用的源代码并生成背景透明的png:

cairo_surface_t *surface; // Declarations
cairo_t *cr;

// Creations of the surface
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,3508,2480);

// Contest
cr = cairo_create(surface);

// Moving the plot to center in the page
cairo_translate(cr, 200,200);

// Scaling the plot
cairo_scale(cr,1.8,1.8);

// Make all the necessary actions to produce the plot 
do_drawing(cr);

// write the plot on the file png
cairo_surface_write_to_png(surface, "image.png");

// End
cairo_surface_destroy(surface);
cairo_destroy(cr);

我还使用了表面CAIRO_FORMAT_RGB24,但我获得了一个像绘图一样的黑色矩形。

你能帮助我吗? 谢谢

2 个答案:

答案 0 :(得分:1)

您需要使用不透明背景绘制,请查看cairo_set_source_rgb函数。

答案 1 :(得分:0)

cairo_surface_t *surface; // Declarations
cairo_t *cr;

// Creations of the surface
// XXX: Changed to format RGB24; if you don't need transparency, well.
surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,3508,2480);

// Contest
cr = cairo_create(surface);

// Draw background XXXXXXXXXXXXXXXXXX
cairo_save(cr);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_paint(cr);
cairo_restore(cr);

// Moving the plot to center in the page
cairo_translate(cr, 200,200);

// Scaling the plot
cairo_scale(cr,1.8,1.8);

// Make all the necessary actions to produce the plot 
do_drawing(cr);

// write the plot on the file png
cairo_surface_write_to_png(surface, "image.png");

// End
cairo_surface_destroy(surface);
cairo_destroy(cr);