Recent

Author Topic: Please help screenshot problums  (Read 3974 times)

BenJones

  • Full Member
  • ***
  • Posts: 121
Please help screenshot problums
« on: July 17, 2011, 05:21:57 pm »
Hi all I been racking my head for over an hour with this bit of code, I am trying to make a program to capture a screen shot of my desktop. but my code seems to be produceing a black image., the only way it works if I use text out and write some text on it then it saves ok but i do not want to really do that can someone please have a look at my code incase i done something wrong thanks.

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  dc: HDC;
  img: TImage;
begin

  //Get desktop window dc
  dc := GetDc(GetDesktopWindow);
  //Create the image
  img := Timage.Create(self);
  //set image width and height
  img.Width := screen.Width;
  img.Height := screen.Height;
  //Hide form we do not want it included in our screeny
  Form1.Visible := False;
  sleep(500);
  //Take screen shot of desktop window
  BitBlt(img.Canvas.Handle, 0, 0, img.Width, img.Height, dc, 0, 0, SRCCOPY);

  //img.Canvas.TextOut(90,0,'Screeny shot by ben');
  //save the image
  img.Picture.SaveToFile('c:\out\benny.bmp');

  //Show the main form
  Form1.Visible := True;

  //Clear up time
  ReleaseDc(GetDesktopWindow, dc);
  img.Free;

end;
When Your Dreams Come True.

BlueIcaro

  • Hero Member
  • *****
  • Posts: 595
Remenber, the lazarus wiki is your friend: http://wiki.lazarus.freepascal.org/Main_Page
General questions (several lenguages) http://wiki.lazarus.freepascal.org/

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Please help screenshot problums
« Reply #2 on: July 17, 2011, 05:49:09 pm »
Replace this:

Code: [Select]
BitBlt(img.Canvas.Handle, 0, 0, img.Width, img.Height, dc, 0, 0, SRCCOPY);
by this:

Code: [Select]
img.Picture.Bitmap.LoadFromDevice(dc);

BenJones

  • Full Member
  • ***
  • Posts: 121
SOLVED Please help screenshot problums
« Reply #3 on: July 17, 2011, 06:26:26 pm »
Thanks both it now working.
When Your Dreams Come True.

 

TinyPortal © 2005-2018