CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2005
    Posts
    41

    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!!

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    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");
      
    }

  3. #3
    Join Date
    Apr 2005
    Posts
    41

    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

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    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

  5. #5
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673

    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
    Last edited by CBasicNet; November 18th, 2009 at 08:26 PM. Reason: Saving image link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured