Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

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

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?
1 Solution
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now