Writing gdiplus to graphics file (JPG or PNG)
Hi all
I am wanting to write a collection of GDI+ objects (rectangles, lines etc) and save the resulting diagram to a graphics file (JPG or PNG). I can easily draw what I want to screen by doing the following:
Code:
void CCanvas::DrawModelToScreen()
{
CClientDC dc(this);
Graphics graphics(dc.m_hDC);
SolidBrush whiteBrush(Color(255, 255, 255, 255));
Pen blackPen(Color(255, 0,0,0));
int x = 10;
int y = 10;
int width = 100;
int height = 100;
graphics.FillRectangle(&whiteBrush, x, y, width, height);
graphics.DrawRectangle(&blackPen, x, y, width, height);
....
}
However I have absolutely no idea how write to a JPEG or PNG instead. Please can someone help!!
Re: Writing gdiplus to graphics file (JPG or PNG)
Your easiest option is to use the CImage class (changes to your code in blue below):
Code:
void CCanvas::DrawModelToScreen()
{
CImage img;
img.CreateEx(640,480,24,0);
CDC dc;
dc.Attach(img.GetDC());
//CClientDC dc(this);
Graphics graphics(dc.m_hDC);
SolidBrush whiteBrush(Color(255, 255, 255, 255));
Pen blackPen(Color(255, 0,0,0));
int x = 10;
int y = 10;
int width = 100;
int height = 100;
graphics.FillRectangle(&whiteBrush, x, y, width, height);
graphics.DrawRectangle(&blackPen, x, y, width, height);
img.Save(L"c:\\test.jpg");
}
Re: Writing gdiplus to graphics file (JPG or PNG)
Hi
Thanks very much for the reply. I don't think I can use the CImage class since I am using VC++6.0 Is there a way to write to a JPG file using VC6?
Thanks
Re: Writing gdiplus to graphics file (JPG or PNG)
Yeah, upgrade to a new compiler. If that's not an option, then you are going to have to do quite a bit of extra coding and probably use some third-party libraries (to support JPEG) and it is beyond the scope of this forum.
You might want to take a look at CXImage:
http://www.codeproject.com/KB/graphics/cximage.aspx
Re: Writing gdiplus to graphics file (JPG or PNG)
The OP can create a Graphics object with a image, instead of a DC. Draw with the Graphics object but the drawing would be "invisible" because the drawings are done on a image instead on a DC. Blit this image on to another destination Graphics object, created with a DC, with destination(not source) Graphics object's Graphics.DrawImage(), then you got the image on screen. Then you can save the image as jpg or png.
There are some code to save image by me in this thread, below, which you can refer.
http://www.codeguru.com/forum/showth...&highlight=GDI