Last active
May 6, 2019 15:04
-
-
Save idleuncle/32ddc34d47b7cc7ccba90b910c2080f1 to your computer and use it in GitHub Desktop.
Cairo graphics 转 wxImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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