Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 157147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:27:09+00:00 2026-05-11T10:27:09+00:00

For full screenshots, I use this code: form1.Hide; sleep(500); bmp := TBitmap.Create; bmp.Height :=

  • 0

For full screenshots, I use this code:

form1.Hide; sleep(500); bmp := TBitmap.Create; bmp.Height := Screen.Height; bmp.Width := Screen.Width; DCDesk := GetWindowDC(GetDesktopWindow); BitBlt(bmp.Canvas.Handle, 0, 0, Screen.Width, Screen.Height, DCDesk, 0, 0, SRCCOPY); form1.Show ; FileName := 'Screenshot_'+FormatDateTime('mm-dd-yyyy-hhnnss',now()); bmp.SaveToFile(Format('C:\Screenshots\%s.bmp', [FileName])); ReleaseDC(GetDesktopWindow, DCDesk); bmp.Free; 

How can I convert that to take a screenshot of only the active window.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:27:10+00:00Added an answer on May 11, 2026 at 10:27 am
    1. First of all you have to get the right window. As sharptooth already noted you should use GetForegroundWindow instead of GetDesktopWindow. You have done it right in your improved version.
    2. But then you have to resize your bitmap to the actual size of the DC/Window. You haven’t done this yet.
    3. And then make sure you don’t capture some fullscreen window!

    When I executed your code, my Delphi IDE was captured and as it is on fullscreen by default, it created the illusion of a fullscreen screenshot. (Even though your code is mostly correct)

    Considering the above steps, I was successfully able to create a single-window screenshot with your code.

    Just a hint: You can GetDC instead of GetWindowDC if you are only interested in the client area. (No window borders)

    EDIT: Here’s what I made with your code:

    You should not use this code! Look at the improved version below.

    procedure TForm1.Button1Click(Sender: TObject); const   FullWindow = True; // Set to false if you only want the client area. var   hWin: HWND;   dc: HDC;   bmp: TBitmap;   FileName: string;   r: TRect;   w: Integer;   h: Integer; begin   form1.Hide;   sleep(500);   hWin := GetForegroundWindow;    if FullWindow then   begin     GetWindowRect(hWin,r);     dc := GetWindowDC(hWin) ;   end else   begin     Windows.GetClientRect(hWin, r);     dc := GetDC(hWin) ;   end;    w := r.Right - r.Left;   h := r.Bottom - r.Top;    bmp := TBitmap.Create;   bmp.Height := h;   bmp.Width := w;   BitBlt(bmp.Canvas.Handle, 0, 0, w, h, DC, 0, 0, SRCCOPY);   form1.Show ;   FileName := 'Screenshot_'+FormatDateTime('mm-dd-yyyy-hhnnss',now());   bmp.SaveToFile(Format('C:\Screenshots\%s.bmp', [FileName]));   ReleaseDC(hwin, DC);   bmp.Free; end; 

    EDIT 2: As requested I’m adding a better version of the code, but I’m keeping the old one as a reference. You should seriously consider using this instead of your original code. It’ll behave much nicer in case of errors. (Resources are cleaned up, your form will be visible again, …)

    procedure TForm1.Button1Click(Sender: TObject); const   FullWindow = True; // Set to false if you only want the client area. var   Win: HWND;   DC: HDC;   Bmp: TBitmap;   FileName: string;   WinRect: TRect;   Width: Integer;   Height: Integer; begin   Form1.Hide;   try     Application.ProcessMessages; // Was Sleep(500);     Win := GetForegroundWindow;      if FullWindow then     begin       GetWindowRect(Win, WinRect);       DC := GetWindowDC(Win);     end else     begin       Windows.GetClientRect(Win, WinRect);       DC := GetDC(Win);     end;     try       Width := WinRect.Right - WinRect.Left;       Height := WinRect.Bottom - WinRect.Top;        Bmp := TBitmap.Create;       try         Bmp.Height := Height;         Bmp.Width := Width;         BitBlt(Bmp.Canvas.Handle, 0, 0, Width, Height, DC, 0, 0, SRCCOPY);         FileName := 'Screenshot_' +            FormatDateTime('mm-dd-yyyy-hhnnss', Now());         Bmp.SaveToFile(Format('C:\Screenshots\%s.bmp', [FileName]));       finally         Bmp.Free;       end;     finally       ReleaseDC(Win, DC);     end;   finally     Form1.Show;   end; end; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 91k
  • Answers 91k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I worded my question badly. It works for other accounts… May 11, 2026 at 6:13 pm
  • Editorial Team
    Editorial Team added an answer This is an issue called "two-stage lookup". Basically, since A… May 11, 2026 at 6:13 pm
  • Editorial Team
    Editorial Team added an answer Use the IDs from the server - they are valid… May 11, 2026 at 6:13 pm

Related Questions

I am supposed to provide my users a really simple way of capturing video
A follow-up from this question , I've changed my controller and routing around so
Is there a one button way to save a screenshot directly to a file
Newbie to .NET data apps here, coming from a Visual Foxpro background. I'm planning

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.