绘制的水印图片,可以添加到应用程序。我是添加到gedit上的(GTK3.0)
#include <cairo.h> #include <math.h> cairo_surface_t *surface; cairo_t *cr; void clear_surface (void) { cairo_t *cr; cr = cairo_create (surface); cairo_set_source_rgb (cr, 1, 1, 1); /* 白色 */ cairo_paint (cr); cairo_destroy (cr); } int main(int argc, char *argv[]) { int width = 0, height = 0; surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 340, 80);//创建一个图像外观 cr = cairo_create(surface); cairo_select_font_face(cr, "serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);//设置字体 cairo_set_font_size(cr, 25.0);//设置字体大小 clear_surface();//设置背景色为白色 cairo_move_to(cr, 10.0, 30.0);//从10 30 开始绘制 cairo_set_source_rgb (cr, 0.9, 0.9, 0.9); /* 设置字体颜色 */ cairo_show_text(cr, "机密文件!请勿外传!"); cairo_destroy(cr); cairo_surface_write_to_png(surface, "timg.png"); cairo_surface_destroy(surface); cairo_surface_t *image; cairo_pattern_t *pattern; surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1440, 900); cr = cairo_create(surface); image = cairo_image_surface_create_from_png ("timg.png"); width = cairo_image_surface_get_width (image); height = cairo_image_surface_get_height (image); pattern = cairo_pattern_create_for_surface (image); cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);//平铺 cairo_rotate (cr, M_PI / 4);//旋转多少 cairo_scale (cr, 2, 2);//x指宽度缩放倍数,y指高度缩放的倍数 cairo_translate (cr, -(width+110), -(height+420)); cairo_set_source (cr, pattern); cairo_rectangle (cr, 0, 0, 1440, 900);//绘制一个矩形,起点是0,0,长1440,宽900 cairo_fill (cr); cairo_destroy(cr); cairo_pattern_destroy (pattern); cairo_surface_destroy (image); cairo_surface_write_to_png(surface, "timg.png"); cairo_surface_destroy(surface); return 0; }
Makefile:
tupian: shili.c cc -g -o $@ $< -lm `pkg-config --cflags --libs gtk+-3.0`
好了生成的图片就可以用了,下图是gedit的水印效果。