1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| // saves the graph to a PNG image
void CGraph::saveToPNG(CString path, int width /* = 0 */, int height /* = 0 */)
{
SLayout imgLayout;
imgLayout._is_valid = false;
if (width != 0)
{
// width/height specified
imgLayout._global = RectF(0, 0, (float)width, (float)height);
}
else
{
// no width specified, use the screen layout size
imgLayout._global = m_screen_layout._global;
SizeF size;
imgLayout._global.GetSize(&size);
width = (int)size.Width;
height = (int)size.Height;
}
// creates a bitmap the right size
Bitmap image(width, height);
// creates a Graphics object based on the image
Graphics g(&image);
// smooth drawing with antialias
g.SetSmoothingMode(SmoothingModeAntiAlias);
// fills the background
SolidBrush* b = new SolidBrush(m_cfg._brush_color);
g.FillRectangle(b, getRect(imgLayout._global));
delete b;
// outline uses the fill color when exporting, so no border is visible
Pen* p = new Pen(m_cfg._brush_color);
g.DrawRectangle(p, getRect(imgLayout._global));
delete p;
// draws everything
drawAll(g, imgLayout);
// saves the image.
CLSID pngClsid;
Utils::GetEncoderClsid(L"image/png", &pngClsid);
BSTR finalPath = path.AllocSysString();
image.Save(finalPath, &pngClsid, NULL);
} |
Partager