2016-08-01 134 views
3

我想採取屏幕的特定部分的屏幕截圖。這裏是來「切割」我想在屏幕部分的座標:採取屏幕的特定部分的屏幕截圖

左:442 上衣:440 右:792 底部:520

也就是說,寬度爲350像素和高度的矩形80px。但我不知道如何使用CopyRect來實現這個任務,而是我得到一個空白圖像。這裏是我的代碼:

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; 
+0

http://stackoverflow.com/questions/8360368/how-to-use-copyrect-method-in-delphi – fantaghirocco

回答

7

你在做什麼這裏複製整個屏幕,並繪製它在協調Rect(442,440,792,520);在新的位圖...這是關閉它的畫布。

座標Rect(442,440,792,520)對應於您想要從源位圖獲取的部分。你想複製「進入」了新的位圖,所以RECT Rect(0,0,350,80)

內你可以簡單地調整你的矩形這樣的:

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

你的代碼的其餘似乎是正確的。

+0

非常感謝你:) – delphirules