idleuncle
12/26/2017 - 6:06 AM

Cairo graphics 转 wxImage

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;