delphirules
delphirules

Reputation: 7390

Take screenshot of specific part of screen

I'm trying to take a screenshot of a specific part of the screen. Here is the coordinates of the part of the screen i want to 'cut' :

Left : 442 Top : 440 Right : 792 Bottom : 520

That is, a rectangle of width 350px and height of 80px. But i don't know how to use CopyRect to achieve this task, instead i'm getting a blank image. Here is my code :

function screenshot: boolean;
var
  Bild : TBitmap;
  c: TCanvas;
  rect_source, rect_destination : TRect;
begin
   c := TCanvas.Create;
   bild := tbitmap.Create;
   c.Handle := GetWindowDC(GetDesktopWindow);
   try
     rect_source := Rect(0, 0, Screen.Width, Screen.Height);
     rect_destination := Rect(442,440,792,520);
     Bild.Width := 350;
     Bild.Height := 80;
     Bild.Canvas.CopyRect(rect_destination, c, rect_source);
     Bild.savetofile('c:\users\admin\desktop\screen.bmp');
   finally
    ReleaseDC(0, c.Handle);
     Bild.free;
     c.Free;
   end;
end;

Upvotes: 3

Views: 2855

Answers (1)

Ken Bourassa
Ken Bourassa

Reputation: 6467

What you are doing here is copying the whole screen and draw it at coordinate Rect(442,440,792,520); in your new bitmap... Which is off its canvas.

The coordinate Rect(442,440,792,520) correspond to the part you want to get from the source bitmap. You want to copy it "inside" your new bitmap, so within the rect Rect(0,0,350,80)

You can simply adjust your rect like this :

 rect_source := Rect(442,440,792,520);
 rect_destination := Rect(0,0,350,80);

The rest of your code seems correct.

Upvotes: 7

Related Questions