Solved

Capture part of an application window

Posted on 2006-10-19
4
204 Views
Last Modified: 2010-04-05
Can someone help me to convert the answer given here so that it is not the desktop, but a region of an applications window that is captured:

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20752840.html?query=screen+capture&topics=85#9462172

I think the following definition is suitable:

GrabAppRect(intLeft, intTop, intWidth, intHeight : integer; Fname: string; awHandle: HWND)

intLeft and intTop is the top left pixel position to start the grab from
intWidth and Height are the required capture widths and heights
Fname is the full path and filename of the bmp file to be output.
and awHandle is the window handle of the application I am trying to capture parts of.

I will be wrapping this in a DLL ultimately, which I am OK with doing (I think!)

Thanks

:-)
0
Comment
Question by:AuthorwareXtras
[X]
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
  • 3
4 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17766510

function GrabAppRect(intLeft, intTop, intWidth, intHeight : integer; Fname: string; awHandle: HWND): BOOL;
var  hdcCapture:    HDC;
     bmpCapture:    TBitmap;
begin

  // Set default result
  result:=False;

  // Validate window handle
  if IsWindow(awHandle) then
  begin
     // Get window DC
     hdcCapture:=GetWindowDC(awHandle);
     // Resource protection
     try
        // Create bitmap for capture
        bmpCapture:=TBitmap.Create;
        // Resource protection
        try
           // Set bitmap properties
           bmpCapture.PixelFormat:=pf24bit;
           bmpCapture.Width:=intWidth;
           bmpCapture.Height:=intHeight;
           // Capture to bitmap
           BitBlt(bmpCapture.Canvas.Handle, 0, 0, intWidth, intHeight, hdcCapture, intLeft, intTop, SRCCOPY);
           // Save to output file name
           bmpCapture.SaveToFile(Fname);
           // Success
           result:=True;
        finally
           // Free bitmap
           bmpCapture.Free;
        end;
     finally
        // Release the window DC
        ReleaseDC(awHandle, hdcCapture);
     end;
  end;

end;

Example usage:

  GrabAppRect(0, 0, 200, 100, 'c:\test.bmp', 5703280);
 

---

Regards,
Russell
0
 

Author Comment

by:AuthorwareXtras
ID: 17767399
Thanks....

That doesn'tqwuite work as I expected in that it does not capture the area I expected.  I think somethign around here is wrong:  BitBlt(bmpCapture.Canvas.Handle, 0, 0, intWidth, intHeight, hdcCapture, intLeft, intTop, SRCCOPY);  should the second and third params be 0??

In any case, I was really wanting a re write of the answer I supplied as I don't want to have to use lots of units.   Without compressing it, the DLL produced is 359K with this solution, wheres it is 41K with answer supplied in the link....As the DLL will be downloaded with a web delivered application I want the smallest footprint possible.

If you can rewrite the solution I supplied, the points will be yours!

Thanks again
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17767989

>> I think somethign around here is wrong:  BitBlt(bmpCapture.Canvas.Handle, 0, 0, intWidth, intHeight, hdcCapture, intLeft, intTop, SRCCOPY);  should the second and third params be 0??

They should be zero if you want the output (in the dest bitmap) to start at (0,0). What is the problem by the way? It starts the copy at (intLeft, intTop) in the window that you are capturing; this includes the title bar, window border etc. If thats a problem, then switch the call from GetWindowDC to GetDC(...) to get the client DC.


>> Without compressing it, the DLL produced is 359K with this solution,
I have no idea what units you included then, as only graphics would be required above and beyond the usual suspects (Windows, sysutils, classes)

eg:

library getimg;

uses
  Windows,
  SysUtils,
  Classes,
  Graphics;

{$R *.RES}


function GrabAppRect(intLeft, intTop, intWidth, intHeight : integer; Fname: string; awHandle: HWND): BOOL;
var  hdcCapture:    HDC;
     bmpCapture:    TBitmap;
begin

  // Set default result
  result:=False;

  // Validate window handle
  if IsWindow(awHandle) then
  begin
     // Get window DC
     hdcCapture:=GetWindowDC(awHandle);
     // Resource protection
     try
        // Create bitmap for capture
        bmpCapture:=TBitmap.Create;
        // Resource protection
        try
           // Set bitmap properties
           bmpCapture.PixelFormat:=pf24bit;
           bmpCapture.Width:=intWidth;
           bmpCapture.Height:=intHeight;
           // Capture to bitmap
           BitBlt(bmpCapture.Canvas.Handle, 0, 0, intWidth, intHeight, hdcCapture, intLeft, intTop, SRCCOPY);
           // Save to output file name
           bmpCapture.SaveToFile(Fname);
           // Success
           result:=True;
        finally
           // Free bitmap
           bmpCapture.Free;
        end;
     finally
        // Release the window DC
        ReleaseDC(awHandle, hdcCapture);
     end;
  end;

end;

begin
end.

I will look at converting the mentioned code; but if I do, I make no warranty as to the code usability (bugs/problems etc).

Russell
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 17768121
function GrabAppRect(intLeft, intTop, intWidth, intHeight : integer; Fname: string; awHandle: HWND): BOOL;
var  BmpDC, hBmpBG, ScreenDC, hFile1, BytesWrite: Cardinal;
     BitC: Integer;
     BitInfo1: TBitmapInfo;
     BFH1: TBitmapFileHeader;
     Pbits: pointer;
     PalColors: array[Byte] of TRGBQuad;
begin

BmpDC := CreateCompatibleDC(0);
{get a DC like the Screen Has}
BitC := GetDeviceCaps(BmpDC, BITSPIXEL);
if BitC < 8 then
BitC := 24;
{the GetDeviceCaps gets the Display color depth, since you used the
CreateCompatibleBitmap, I just guess that you want a Bitmap File with
the same color stuff as the Screen}

PBits := nil;
ZeroMemory(@BitInfo1, sizeOf(BitInfo1));

{You use a Device Dependent Bitmap from CreateCompatibleBitmap, but a Bitmap
file needs to be Device Independent, It would be useless to create a Bitmap
with CreateCompatibleBitmap, because you would still need to create a
DIB with CreateDIBSection or convert the DDB to DIB another way
I Just create A DIB and save it to file}
with BitInfo1.bmiHeader do
  begin
  biSize := sizeOf(BitInfo1.bmiHeader);
  biBitCount    := BitC;
{you can set the biBitCount to the color depth you need as a value of
1, 4, 8, 16, 24, or 32,
WARNING but if you use 1 or 4, you will need to change the
formular for biSizeImage := (biWidth * (BitC div 8)) * biHeight}
  biCompression := BI_RGB;
  biPlanes := 1;
  biWidth := intWidth;
  biHeight := intHeight;
  biSizeImage := (biWidth * (BitC div 8)) * biHeight;
  end;
{the CreateDIBSection needs the info in the BitInfo1 to make a Device
Independent Bitmap}
hBmpBG := CreateDIBSection(BmpDC, BitInfo1, DIB_RGB_COLORS, Pbits, 0, 0);

SelectObject(BmpDC, hBmpBG);

{if the color Depth is 8 bit then you will need to get the Palette}
if BitC = 8 then
  begin
  BitInfo1.bmiHeader.biClrUsed := 256;
  GetDIBColorTable(BmpDC, 0, 255, PalColors);
  end else
  BitInfo1.bmiHeader.biClrUsed := 0;

ScreenDC := GetWindowDC(awHandle);
BitBlt(BmpDC,0,0, BitInfo1.bmiHeader.biWidth,
       BitInfo1.bmiHeader.biHeight, ScreenDC,intLeft,intTop,SRCCOPY);
{this copies the screen to the bitmap}
DeleteDC(BmpDC);
ReleaseDC(awHandle, ScreenDC);

ZeroMemory(@BFH1, sizeOf(BFH1));
{start building your Bitmap file with the Bitmap File Header}
BFH1.bfType := $4D42;  // ASCII for 'BM'
BFH1.bfOffBits := sizeof(BFH1)+BitInfo1.bmiHeader.biSize+
                  (BitInfo1.bmiHeader.biClrUsed * SizeOf(TRGBQUAD));
BFH1.bfSize := BFH1.bfOffBits + BitInfo1.bmiHeader.biSizeImage;

{create an API File}
hFile1 := CreateFile(PChar(Fname),GENERIC_WRITE,FILE_SHARE_READ, nil,
          CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN, 0);
if hFile1 <> INVALID_HANDLE_VALUE then
  begin
  {you should include some tests here to see if the file parts are written
  but I did not do that to make this a little less complex}
  WriteFile(hFile1,BFH1, SizeOf(BFH1), BytesWrite, nil);
{write BMP file header}
  WriteFile(hFile1, BitInfo1.bmiHeader, SizeOf(BitInfo1.bmiHeader), BytesWrite, nil);
{write Bmp Info Header}
  if BitInfo1.bmiHeader.biClrUsed > 0 then
  WriteFile(hFile1,PalColors, BitInfo1.bmiHeader.biClrUsed * SizeOf(TRGBQUAD), BytesWrite, nil);
{write Palette color defintions if there are biClrUsed}
  WriteFile(hFile1, Pbits^, BitInfo1.bmiHeader.biSizeImage, BytesWrite, nil);
{write Pixel color Bits defintions in the Pointer Pbits}
  CloseHandle(hFile1);
  end;
DeleteObject(hBmpBG);
{you can NOT deleteObject your bitmap until After the Pbits are written to file}

  result:=True;

end;
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

623 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