开罗图书馆:生成一个白色背景的 png 文件

Cairo Library: produce a png file with white background

我在 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 但是我得到了一个黑色的矩形,就像绘图一样。

你能帮帮我吗? 谢谢

你需要用不透明的背景来绘制,看看cairo_set_source_rgb函数。

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);