4e554c4c
4e554c4c

Reputation: 549

Scale cairo image surface

I have a cairo_surface_t image surface that contains an arbitrary sized icon, and would like to scale it so that it fits into a window that I am drawing into. The window has a cairo_t drawing context that can be painted into. Example code is below

cairo_surface_t *image;
double scale = cairo_image_get_surface_height(image) / window->height;
// Scale here
cairo_set_source_surface(cairo, image, 0, 0);
cairo_paint(cairo);

Upvotes: 2

Views: 6456

Answers (1)

Amol
Amol

Reputation: 47

You can use the cairo_scale API. Example: cairo_scale(cairo, scale_width, scale_height);

I will do this like below:

  1. Resize the image using any algorithm.
  2. cairo_surface_t *surf = cairo_image_surface_create_for_data(resize_image_data, CAIRO_FORMAT_RGB24, resize_image_width, resize_image_height, image_stride);
  3. Create cairo_t object cr.
  4. cairo_set_source_surface(cr, surf, 0, 0);
  5. cairo_paint(cr);

Upvotes: 3

Related Questions