Delphi – screenshot of the screen specific part of the screen

I am trying to take a screenshot of a specific part of the screen. Here are the coordinates of the part of the screen I want to “cut”:

Left: 442< br>Top: 440
Right: 792
Bottom: 520

That is to say, a rectangle with a width of 350px and a height of 80px. But I don’t know how to use CopyRect to accomplish this Task instead of getting a blank image. This is my code:

function screenshot: boolean;
var
Bild: TBitmap;< br /> 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;

What you’re doing here is copy the entire screen and place it at the coordinates Rect( 442,440,792,520) drawn at It; in your new bitmap… which is not on its canvas.

The coordinate Rect(442,440,792,520) corresponds to the part to be obtained from the source bitmap. You want to copy it to the new Inside the bitmap, so in rect Rect(0,0,350,80)

You can adjust the rectangle like this:

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

The rest of the code seems to be correct.

I Trying to take a screenshot of a specific part of the screen. Here are the coordinates of the part of the screen I want to “cut”:

Left: 442
Upper: 440
Right: 792< br>Below: 520

That is to say, a rectangle with a width of 350px and a height of 80px. But I don’t know how to use CopyRect to accomplish this task instead of getting a blank image. This 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\des ktop\screen.bmp');
finally
ReleaseDC(0, c.Handle);
Bild.free;
c.Free;
end;
end;

What you are doing here is to copy the entire screen and draw it at coordinates Rect(442,440,792,520); in your new bitmap… Which is not on its canvas.

The coordinate Rect(442,440,792,520) corresponds to the part to be obtained from the source bitmap. You want to copy it to the new bitmap, so in rect Rect( 0,0,350,80)

You can adjust the rectangle like this:

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

The rest of the code seems to be correct.

Leave a Comment

Your email address will not be published.