Solved

Capture part of an application window

Posted on 2006-10-19
4
196 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
  • 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now