Solved

Save and retrieve a graph

Posted on 2000-02-21
3
196 Views
Last Modified: 2010-04-10
How can  I save a graph? The graph is plotted using pDC->MoveTo and LineTo. It has text also which is written using pDC->TextOut command. I'm using AppWIzard for this program. Please guide me through each single step. Thanks.
0
Comment
Question by:kitlee
3 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2542744
You could save the drawing operations using a windows meta file.  

I'm not familiar with the MFC way of dealing with metafiles, but it appears to be done through CMetaFileDC.
0
 
LVL 1

Expert Comment

by:nutsnuts
ID: 2542817
Which graph format do you want to save it as?
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 100 total points
ID: 2544505
If you want to save your graph as a bitmap picture, then create a DIB bitmap (I like 24Bit DIBs as they are easier), select it into a CDC devicec context and then call your OnDraw code to draw onto the bitmap (just make sure your bitmap is big enough and/or that your drawing code is told how big to draw the graph).  Then, when finished, write the bitmap to a file.  This is not directly suported by windows, but there is sample code on the MSKB for how to write out DIB files.

Otherwise, just write out your data by serialising your document (which should be where you keep your data).  Then you can view the data by loading a document into your app.  Even nicer, when you build your app, make it an OLE server.  Then you can do things like embed your graphs in Word documents etc.

PS: For 24Bit DIB bitmaps, I have a class derived from CBitmap.  It has a member function to write the bitmap to a CFile.  This is the code I use...
      // setup file header
      BITMAPFILEHEADER hdr;
      hdr.bfType = 0x4d42;      // 0x42 = "B", 0x4d = "M"
      hdr.bfReserved1 = 0;
      hdr.bfReserved2 = 0;

      BITMAPINFOHEADER bitmapinfoheader;
      GetBITMAPINFOHEADER(bitmapinfoheader);

      // Compute the offset to the array of color indices.
      hdr.bfOffBits = (DWORD) (
            sizeof(BITMAPFILEHEADER) +
            bitmapinfoheader.biSize +
            bitmapinfoheader.biClrUsed * sizeof (RGBQUAD)
      );

      // Compute the size of the entire file.
      hdr.bfSize = hdr.bfOffBits + bitmapinfoheader.biSizeImage;

      bool ok = false;
      TRY {
            // Copy the BITMAPFILEHEADER into the .BMP file.
            file.WriteHuge(&hdr,sizeof(hdr));
            // Copy the BITMAPINFOHEADER and RGBQUAD array into the file.
            file.WriteHuge(&bitmapinfoheader,sizeof(bitmapinfoheader) + bitmapinfoheader.biClrUsed * sizeof (RGBQUAD));
            // Copy the array of color indices into the .BMP file.
            file.WriteHuge(m_pBits,bitmapinfoheader.biSizeImage);

            ok = true;
      } CATCH_ALL (e) {
      } END_CATCH_ALL
      return ok;

If you want a graphic formats other than BMP files, then you'll probably need a third party graphics library (or track down some source code).

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question