Skip to content

Instantly share code, notes, and snippets.

@idleuncle
Last active May 6, 2019 15:04
Show Gist options
  • Save idleuncle/32ddc34d47b7cc7ccba90b910c2080f1 to your computer and use it in GitHub Desktop.
Save idleuncle/32ddc34d47b7cc7ccba90b910c2080f1 to your computer and use it in GitHub Desktop.
Cairo graphics 转 wxImage
wxRect rect(0,0,200,200);
unsigned int image_buffer_len = rect.width * rect.height * 4;
unsigned char *image_buffer = (unsigned char*)new unsigned char[image_buffer_len];
cairo_surface_t *cairo_surface = cairo_image_surface_create_for_data(image_buffer, CAIRO_FORMAT_RGB32, rect.width, rect.height, rect.width * 4);
cairo_t *cairo_image = cairo_create(cairo_surface);
Render(cairo_image, rect.width, rect.height);
unsigned char *output = (unsigned char*)new unsigned char[image_buffer_len];
int offset = 0;
for (size_t count = 0; count < image_buffer_len; count += 4){
int r = *(image_buffer + count + 2);
*(output + offset) = r;
offset++;
int g = *(image_buffer + count + 1);
*(output + offset) = g;
offset++;
int b = *(image_buffer + count + 0);
*(output + offset) = b;
offset++;
}
wxImage img(rect.width, rect.height, output, true);
img.Save(wxT("/tmp/xxx.png"), wxBITMAP_TYPE_PNG);
cairo_destroy(cairo_image);
cairo_surface_destroy(cairo_surface);
delete[] image_buffer;
delete[] output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment