Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Capture part of an application window

Posted on 2006-10-19
4
Medium Priority
?
206 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 2000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

664 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