C 如何在开罗绘制切片图像?
我目前正在使用以下代码绘制切片图像:C 如何在开罗绘制切片图像?,c,cairo,C,Cairo,我目前正在使用以下代码绘制切片图像: static void DrawImage(cairo_t *ctx,int x,int y,int w,int h,cairo_surface_t *img){ cairo_set_source_surface(ctx, img, 0, 0); cairo_rectangle(ctx, x, y, w, h); cairo_clip(ctx); cairo_paint(ctx); cairo_reset_clip(c
static void DrawImage(cairo_t *ctx,int x,int y,int w,int h,cairo_surface_t *img){
cairo_set_source_surface(ctx, img, 0, 0);
cairo_rectangle(ctx, x, y, w, h);
cairo_clip(ctx);
cairo_paint(ctx);
cairo_reset_clip(ctx);
}
void DrawThreeSliceImage(cairo_t* ctx,Point p,int width,cairo_surface_t *a,cairo_surface_t *b,cairo_surface_t *c){
DrawImage(ctx, p.x, p.y, cairo_image_surface_get_width(a), cairo_image_surface_get_height(a),a);
DrawImage(ctx, p.x+cairo_image_surface_get_width(a), p.y, width-(cairo_image_surface_get_width(a)+cairo_image_surface_get_width(c)), cairo_image_surface_get_height(b),b);
DrawImage(ctx, p.x+(width-cairo_image_surface_get_width(c)), p.y, cairo_image_surface_get_width(c), cairo_image_surface_get_height(c),c);
}
但是,没有输出。我尝试过对xlib和png曲面进行渲染,这两种方法似乎都不起作用,也就是说没有绘制任何内容。此外,我已经将输入曲面写入png文件,它们似乎正在工作
谢谢。对于那些可能有类似问题的人,以下代码对我有效:
void DrawImage(cairo_t *ctx,int x,int y,int w,int h,cairo_surface_t *img){
/*if((w||h) || (w==cairo_image_surface_get_width(img)&&h==cairo_image_surface_get_height(img))){
cairo_save(ctx);
cairo_translate(ctx, x, y);
cairo_scale(ctx, ((double)w)/((double)cairo_image_surface_get_width(img)), ((double)h)/((double)cairo_image_surface_get_height(img)));
cairo_pattern_t *pattern=cairo_pattern_create_for_surface(img);
cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
cairo_set_source(ctx, pattern);
cairo_rectangle(ctx, 0,0, cairo_image_surface_get_width(img), cairo_image_surface_get_height(img));
cairo_fill(ctx);
cairo_pattern_destroy(pattern);
cairo_restore(ctx);
}else{
cairo_set_source_surface(ctx, img, x,y);
cairo_rectangle(ctx, x,y, cairo_image_surface_get_width(img), cairo_image_surface_get_height(img));
cairo_fill(ctx);
}*/
cairo_save(ctx);
cairo_translate(ctx, x, y);
cairo_pattern_t *pattern=cairo_pattern_create_for_surface(img);
cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
cairo_set_source(ctx, pattern);
cairo_rectangle(ctx, 0, 0, w?w:cairo_image_surface_get_width(img), h?h:cairo_image_surface_get_height(img));
cairo_fill(ctx);
cairo_pattern_destroy(pattern);
cairo_restore(ctx);
}
void DrawThreeSliceImage(cairo_t* ctx,Point p,int width,cairo_surface_t* imgs[3]){
#define a (imgs[0])
#define b (imgs[1])
#define c (imgs[2])
DrawImage(ctx, p.x, p.y, cairo_image_surface_get_width(a), cairo_image_surface_get_height(a),a);
DrawImage(ctx, p.x+cairo_image_surface_get_width(a), p.y, width-(cairo_image_surface_get_width(a)+cairo_image_surface_get_width(c)), cairo_image_surface_get_height(b),b);
DrawImage(ctx, p.x+(width-cairo_image_surface_get_width(c)), p.y, cairo_image_surface_get_width(c), cairo_image_surface_get_height(c),c);
#undef a
#undef b
#undef c
}
对于那些可能有类似问题的人,此代码适用于我:
void DrawImage(cairo_t *ctx,int x,int y,int w,int h,cairo_surface_t *img){
/*if((w||h) || (w==cairo_image_surface_get_width(img)&&h==cairo_image_surface_get_height(img))){
cairo_save(ctx);
cairo_translate(ctx, x, y);
cairo_scale(ctx, ((double)w)/((double)cairo_image_surface_get_width(img)), ((double)h)/((double)cairo_image_surface_get_height(img)));
cairo_pattern_t *pattern=cairo_pattern_create_for_surface(img);
cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
cairo_set_source(ctx, pattern);
cairo_rectangle(ctx, 0,0, cairo_image_surface_get_width(img), cairo_image_surface_get_height(img));
cairo_fill(ctx);
cairo_pattern_destroy(pattern);
cairo_restore(ctx);
}else{
cairo_set_source_surface(ctx, img, x,y);
cairo_rectangle(ctx, x,y, cairo_image_surface_get_width(img), cairo_image_surface_get_height(img));
cairo_fill(ctx);
}*/
cairo_save(ctx);
cairo_translate(ctx, x, y);
cairo_pattern_t *pattern=cairo_pattern_create_for_surface(img);
cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
cairo_set_source(ctx, pattern);
cairo_rectangle(ctx, 0, 0, w?w:cairo_image_surface_get_width(img), h?h:cairo_image_surface_get_height(img));
cairo_fill(ctx);
cairo_pattern_destroy(pattern);
cairo_restore(ctx);
}
void DrawThreeSliceImage(cairo_t* ctx,Point p,int width,cairo_surface_t* imgs[3]){
#define a (imgs[0])
#define b (imgs[1])
#define c (imgs[2])
DrawImage(ctx, p.x, p.y, cairo_image_surface_get_width(a), cairo_image_surface_get_height(a),a);
DrawImage(ctx, p.x+cairo_image_surface_get_width(a), p.y, width-(cairo_image_surface_get_width(a)+cairo_image_surface_get_width(c)), cairo_image_surface_get_height(b),b);
DrawImage(ctx, p.x+(width-cairo_image_surface_get_width(c)), p.y, cairo_image_surface_get_width(c), cairo_image_surface_get_height(c),c);
#undef a
#undef b
#undef c
}