Screenshot code

Hi

I am trying to add a piece of software into my Visual C++ program to take a screenshot of the desktop screen several times and save the images in a folder. The program displays animated image with OpenGL

The code should be accommodated in a loop so that each time the loop is increased a function is called to take a screenshot of the movie displayed on the screen automatically and save it in a file with different name to the others, finally I would have several static image files taken at different moments and saved with a sequence of file names.

I tried to use many code I found on the web but they did not work, some of them created and saved files but could not open them and they were almost empty.

My question is: can anyone help me in this ? I need a code to be added to my program to call a function to take screenshot of my screen at specific points and save the images in different files with different names so that I can open them later with QuickTime and display them in a sequence as a movie

your help would be really appreciated

regards
Here's some code I used for my Wallpapers program... It will do what you want. You just need to translate it to VC++ from Delphi. (Don't worry, it really isn't that hard, since the only VCL component you need is a TBitmap and the rest is pure Win32.)

    Documentation (Commentary)
96
97
98
99
100
101
102
103
104
105
106
  function ScreenShot: tBitmap
    Take a snapshot of the display.

    arguments
      win   (optional) The window to take a snapshot of. If not specified (or
                       if zero) the entire desktop is imaged.
      clip  (optional) The area of the target window to image. If not
                       specified (or if invalid) the entire target is imaged.

    references
      < http://delphi.about.com/od/adptips2006/qt/captureactive.htm > 

    Interface (Prototypes)
139
140
141
142
function ScreenShot:                           tBitmap; overload;
function ScreenShot(            clip: tRect ): tBitmap; overload;
function ScreenShot( win: hWnd              ): tBitmap; overload;
function ScreenShot( win: hWND; clip: tRect ): tBitmap; overload;

    Implementation
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//----------------------------------------------------------------------------
function ScreenShot: tBitmap;
  var r: tRect;
  begin
  r      := rect( 0, 0, 0, 0 );
  result := ScreenShot( 0, r )
  end;

//----------------------------------------------------------------------------
function ScreenShot( clip: tRect ): tBitmap;
  begin
  result := ScreenShot( 0, clip )
  end;

//----------------------------------------------------------------------------
function ScreenShot( win: hWnd ): tBitmap;
  var r: tRect;
  begin
  r      := rect( 0, 0, 0, 0 );
  result := ScreenShot( win, r )
  end;

//----------------------------------------------------------------------------
function ScreenShot( win: hWND; clip: tRect ): tBitmap;
  var
    dc: hDC;
    r:  tRect;
  begin
  // Get the device context and window size
  if win = 0
    then begin    // entire desktop
         win      := GetDesktopWindow;
         dc       := GetDC( win );
         r.left   := 0;
         r.top    := 0;
         r.right  := GetDeviceCaps( dc, HORZRES );
         r.bottom := GetDeviceCaps( dc, VERTRES )
         end
    else begin    // specific window
         dc := GetWindowDC( win );
         GetWindowRect( win, r );
         dec( r.right, r.left );
         dec( r.bottom, r.top );
         r.left := 0;
         r.top  := 0
         end;
  try
    // Clip the specified rects --> r
    if not IsRectEmpty( clip ) then
    if not IntersectRect( r, r, clip )
      then RaiseLastOSError;

    // Create and fill the result
    result := tBitmap.create;
    try
      result.width  := r.right -r.left;
      result.height := r.bottom -r.top;
      if not BitBlt(
        result.canvas.handle,
        0,
        0,
        result.width,
        result.height,
        dc,
        r.left,
        r.top,
        SRCCOPY
        ) then RaiseLastOSError

    except result.free; raise end

  finally ReleaseDC( win, dc ) end
  end;

Once you have your screenshot in a TBitmap, you'll need to use the SaveToFile() method to write it to disk as a .BMP file.

Playing with Quicktime is not in my knowledgebase.

Good luck!
Duoas

Thank you very much for your help

I am trying your suggestion

Topic archived. No new replies allowed.