本文主要是介绍cairo 绘制水印背景图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
绘制的水印图片,可以添加到应用程序。我是添加到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.ccc -g -o $@ $< -lm `pkg-config --cflags --libs gtk+-3.0`
好了生成的图片就可以用了,下图是gedit的水印效果。
这篇关于cairo 绘制水印背景图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!