Solved

Save and retrieve a graph

Posted on 2000-02-21
3
193 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
Comment Utility
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
Comment Utility
Which graph format do you want to save it as?
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 100 total points
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now