Skip to content

Instantly share code, notes, and snippets.

@lag945
Last active September 14, 2022 02:30
Show Gist options
  • Save lag945/783eaf72d1b896fcc2e1ac62af3da528 to your computer and use it in GitHub Desktop.
Save lag945/783eaf72d1b896fcc2e1ac62af3da528 to your computer and use it in GitHub Desktop.
C++ exmaple convert svg to png with resvg library.
#include <iostream>
#include <stb_image_write.h>
#include <resvg.h>
int main()
{
resvg_options* opt = resvg_options_create();
resvg_options_load_system_fonts(opt);
resvg_render_tree* tree;
int err = resvg_parse_tree_from_file("bboxes.svg", opt, &tree);
resvg_options_destroy(opt);
if (err == RESVG_OK)
{
resvg_size size = resvg_get_image_size(tree);
int width = (int)size.width;
int height = (int)size.height;
resvg_fit_to fit_to = { RESVG_FIT_TO_TYPE_ORIGINAL, 1 };
char* pData = new char[width * height * 4];
memset(pData, 0, width * height * 4);//default alhpa=0 and color=black
resvg_render(tree, fit_to, resvg_transform_identity(), width, height, (char*)pData);//RGBA
err = stbi_write_png("bboxes.svg.png", width, height, 4, pData, width * 4);
delete[] pData;
}
else
{
std::cout << "Error id: " << err << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment