Draw with mouse and store the "piece of art" as jpg or bmp

Posted on 2003-03-13
Medium Priority
Last Modified: 2013-11-20
I have to draw something with my mouse and store the "result" in an image (jpg, bmp, tif - something like this).

Is there an OLE Object that can capture mouse events, draw the movement of the mouse and store it's content in an image or something like this?

To implement a "drawing" with the mouse is not the problem (I could just use one of the well known Scribble-examples, right?), but how do I convert the output into an image?
Question by:Koinseb
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment

Accepted Solution

gelbert earned 300 total points
ID: 8129981
1.Create CBitmap object ( for example: objBMP) and initialize it using CreateCompatibleBitmap() of this object with DC of the window which user paints on.
2. Call SelectObject() with window DC and pointer to your new bitmap.
3. Let user do painting.
4. Call SelectObject() again to restore default DC's bitmap. Noew you should have bitmap store in your new bitmap object (objBMP).
5. Detach bitmap handle from bitmap object (objBMP)
6. Call following function to save bitmap as file. Provide file name, bitmap handle as parameters

BOOL SaveBitmap( LPCSTR lpFileName, HBITMAP hBitmap, HPALETTE hPal = NULL )
   BOOL bResult = FALSE;

   PICTDESC stPictDesc;
   stPictDesc.cbSizeofstruct = sizeof(PICTDESC);
   stPictDesc.picType = PICTYPE_BITMAP;
   stPictDesc.bmp.hbitmap = hBitmap;
   stPictDesc.bmp.hpal = hPal;

   LPPICTURE pPicture;
   HRESULT hr = OleCreatePictureIndirect( &stPictDesc, IID_IPicture, FALSE, reinterpret_cast<void**>(&pPicture) );
   if ( SUCCEEDED(hr) )
      LPSTREAM pStream;
      hr = CreateStreamOnHGlobal( NULL, TRUE, &pStream );
      if ( SUCCEEDED(hr) )
         long lBytesStreamed = 0;
         hr = pPicture->SaveAsFile( pStream, TRUE, &lBytesStreamed );
         if ( SUCCEEDED(hr) )
            HANDLE hFile = CreateFile( lpFileName,
                                       NULL );
            if ( hFile )
               HGLOBAL hMem = NULL;
               GetHGlobalFromStream( pStream, &hMem );
               LPVOID lpData = GlobalLock( hMem );

               DWORD dwBytesWritten;
               bResult = WriteFile( hFile, lpData, lBytesStreamed, &dwBytesWritten, NULL );
               bResult &= ( dwBytesWritten == (DWORD)lBytesStreamed );

               // clean up
         // clean up        
      // clean up      

   return bResult;  


Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

764 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