was ist neu ¦  programmier tips ¦  indy artikel ¦  intraweb artikel ¦  informationen ¦  links ¦  interviews
 sonstiges ¦  tutorials ¦  Add&Win Gewinnspiel

Tips (1541)

Dateien (137)
Datenbanken (90)
Drucken (35)
Grafik (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Mathematik (76)
Multimedia (45)
Oberfläche (107)
Objekte/
ActiveX (51)

OpenTools API (3)
Sonstiges (126)
Strings (83)
System (266)
VCL (242)

Tips sortiert nach
Komponente


Tip suchen

Tip hinzufügen

Add&Win Gewinnspiel

Werbung

44 Visitors Online


 
...einen Desktop Screenshot erzeugen?
Autor: Simon Grossenbacher
Homepage: http://www.swissdelphicenter.ch
[ Tip ausdrucken ]  

Tip Bewertung (31):  
     


{---------------------1---------------------}
function GetScreenShot: TBitmap;
var
  
Desktop: HDC;
begin
  
Result  := TBitmap.Create;
  Desktop := GetDC(0);
  try
    try
      
Result.PixelFormat := pf32bit;
      Result.Width := Screen.Width;
      Result.Height := Screen.Height;
      BitBlt(Result.Canvas.Handle, 0, 0, Result.Width, Result.Height, Desktop, 0, 0, SRCCOPY);
      Result.Modified := True;
    finally
      
ReleaseDC(0, Desktop);
    end;
  except
    
Result.Free;
    Result := nil;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  
Image1.Picture.Bitmap := GetScreenShot;
end;


{---------------------2---------------------}
procedure TForm1.Button1Click(Sender: TObject);
var DCDesk: HDC; // hDC of Desktop
  
bmp: TBitmap;
begin
  
{Create a bitmap}
  
bmp := TBitmap.Create;

  {Set a bitmap sizes}
  
bmp.Height := Screen.Height;
  bmp.Width := Screen.Width;

  {Get a desktop DC handle - handle of a display device context}
  
DCDesk := GetWindowDC(GetDesktopWindow);

  {Copy to any canvas, here canvas of an image}
  
BitBlt(bmp.Canvas.Handle, 0, 0, Screen.Width, Screen.Height,
         DCDesk, 0, 0, SRCCOPY);

  {Save the bitmap}
  
bmp.SaveToFile('ScreenShot.bmp');

  {Release desktop DC handle}
  
ReleaseDC(GetDesktopWindow, DCDesk);

  {Release a bitmap}
  
bmp.Free;
end;

{---------------------3---------------------}
{
  If you will try to capture the screen to bitmap using BitBlt function
  and have a layered (transparent)  window visible,
  you will get only a picture of the screen without this window.
  To fix it we have to use the new raster-operation code for BitBlt
  function CAPTUREBLT that introduced in Windows 2000 for including
  any windows that are layered on top of your window in the resulting image.
}

procedure CaptureScreen(AFileName: string);
const
  
CAPTUREBLT = $40000000;
var
  
hdcScreen: HDC;
  hdcCompatible: HDC;
  bmp: TBitmap;
  hbmScreen: HBITMAP;
begin
  
// Create a normal DC and a memory DC for the entire screen. The
  // normal DC provides a "snapshot" of the screen contents. The
  // memory DC keeps a copy of this "snapshot" in the associated
  // bitmap.

  
hdcScreen := CreateDC('DISPLAY', nilnilnil);
  hdcCompatible := CreateCompatibleDC(hdcScreen);
  // Create a compatible bitmap for hdcScreen.

  
hbmScreen := CreateCompatibleBitmap(hdcScreen,
    GetDeviceCaps(hdcScreen, HORZRES),
    GetDeviceCaps(hdcScreen, VERTRES));

  // Select the bitmaps into the compatible DC.
  
SelectObject(hdcCompatible, hbmScreen);
  bmp := TBitmap.Create;
  bmp.Handle := hbmScreen;
  BitBlt(hdcCompatible,
    0, 0,
    bmp.Width, bmp.Height,
    hdcScreen,
    0, 0,
    SRCCOPY or CAPTUREBLT);

  bmp.SaveToFile(AFileName);
  bmp.Free;
  DeleteDC(hdcScreen);
  DeleteDC(hdcCompatible);
end;

 

Bewerten Sie diesen Tipp:

dürftig
ausgezeichnet


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners