Web Design that taps into the haromny and vision of your dreams.

Grab Screen Shot with Delphi

Delphi Screen Shots

Written By on in Coding

154 words, estimated reading time 1 minutes.

This short procedure will capture the current screen contents to a Bitmap which you can manipulate or save to disk.

procedure GrabScreen(bm: TBitMap; gt : GrabType);
var
  DestRect, SourceRect: TRect;
  h: THandle;
  hdcSrc : THandle;
  pt : TPoint;
begin
  case(gt) of
    GTWINDOW, GTCLIENT : h := GetForeGroundWindow;
    GTSCREEN : h := GetDesktopWindow;
  else h := 0;
  end;
  if h <> 0 then
  begin
    try
      if gt = GTCLIENT then
      begin
        hdcSrc := GetDC(h); // use this for ClientRect
        Windows.GetClientRect(h,SourceRect);
      end
      else
      begin
          hdcSrc := GetWindowDC(h);
          GetWindowRect(h, SourceRect);
      end;
        bm.Width  := SourceRect.Right - SourceRect.Left;
        bm.Height := SourceRect.Bottom - SourceRect.Top;
        DestRect := Rect(0, 0, SourceRect.Right - SourceRect.Left, SourceRect.Bottom - SourceRect.Top);
          StretchBlt(bm.Canvas.Handle, 0, 0, bm.Width,
            bm.Height, hdcSrc,
            0,0,SourceRect.Right - SourceRect.Left,
            SourceRect.Bottom - SourceRect.Top,
            SRCCOPY);
        if gt = GTCLIENT then
        begin
           pt.X :=SourceRect.Left;
           pt.Y :=SourceRect.Top;
           // call Windows.ClientToScreen() to translate X and Y
           Windows.ClientToScreen(h, pt);
           //DrawCursor(bm,pt.X, pt.Y);
        end
        //
        else
          //DrawCursor(bm,SourceRect.Left, SourceRect.Top);
    finally
      ReleaseDC(0, hdcSrc);
    end;
  end;
end;

Last updated on: Saturday 24th June 2017

 

Comments

There are no comments for this post. Be the first!

 

Leave a Reply

Your email address will not be published.





If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

 

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me