Recent

Author Topic: [SOLVED] Creating and Saving a Screenshot  (Read 9749 times)

RobWitcher

  • New Member
  • *
  • Posts: 31
[SOLVED] Creating and Saving a Screenshot
« on: January 08, 2013, 09:02:03 pm »
Hi

(I know this is my third forum topic in a row  :-[ )
I have been wondering if there is an easy way to take a screenshot and save it to a specified folder?
I tried following a few online tutorials but they are fairly complexed and poorly explained.
I don't suppose anyone here could talk me through how to do it and/or maybe provide a working source code for a program doing just this?

If you can, thanks.
« Last Edit: January 10, 2013, 11:40:52 pm by RobWitcher »
-Rob

Sanchoyzer

  • Jr. Member
  • **
  • Posts: 65
Re: Creating and Saving a Screenshot
« Reply #1 on: January 09, 2013, 12:22:05 am »
try something like
Code: [Select]
var
  ScreenDC: HDC;
  SaveBitmap: TBitmap;
begin
  SaveBitmap := TBitmap.Create;
  with SaveBitmap do
  begin
    Height := Screen.Height;
    Width := Screen.Width;
  end;

  ScreenDC := GetDC(0);
  SaveBitmap.LoadFromDevice(ScreenDC);
  ReleaseDC(0, ScreenDC);
...
« Last Edit: January 09, 2013, 12:25:28 am by Sanchoyzer »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Creating and Saving a Screenshot
« Reply #2 on: January 09, 2013, 02:24:38 am »
well if you have tried searching the forums for "screen capture" you would have seen the following thread.

http://www.lazarus.freepascal.org/index.php?topic=11136.0

I think you should find everything you need there also what Sanchoyzer posted I know that it works in windows I do not know how well it works on other OSes though.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Creating and Saving a Screenshot
« Reply #3 on: January 09, 2013, 12:26:42 pm »
Quote
also what Sanchoyzer posted I know that it works in windows I do not know how well it works on other OSes though.
It works cross platform if LCL* units are used.

RobWitcher

  • New Member
  • *
  • Posts: 31
Re: Creating and Saving a Screenshot
« Reply #4 on: January 09, 2013, 08:42:15 pm »
try something like
Code: [Select]
var
  ScreenDC: HDC;
  SaveBitmap: TBitmap;
begin
  SaveBitmap := TBitmap.Create;
  with SaveBitmap do
  begin
    Height := Screen.Height;
    Width := Screen.Width;
  end;

  ScreenDC := GetDC(0);
  SaveBitmap.LoadFromDevice(ScreenDC);
  ReleaseDC(0, ScreenDC);
...

Thanks, I get a few errors when compiling so I was just wondering if there is anything specific I need in the 'uses' section?

Quote
unit1.pas(27,16) Error: Identifier not found "HDC"
unit1.pas(27,16) Error: Error in type definition
unit1.pas(38,20) Error: Identifier not found "GetDC"
unit1.pas(39,37) Error: Incompatible type for arg no. 1: Got "<erroneous type>", expected "LongWord"
rasterimage.inc(348,24) Hint: Found declaration: TRasterImage.LoadFromDevice(LongWord);
unit1.pas(40,12) Error: Identifier not found "ReleaseDC"
unit1.pas(45) Fatal: There were 5 errors compiling module, stopping
-Rob

RobWitcher

  • New Member
  • *
  • Posts: 31
Re: Creating and Saving a Screenshot
« Reply #5 on: January 09, 2013, 08:43:24 pm »
well if you have tried searching the forums for "screen capture" you would have seen the following thread.

http://www.lazarus.freepascal.org/index.php?topic=11136.0

I think you should find everything you need there also what Sanchoyzer posted I know that it works in windows I do not know how well it works on other OSes though.

Thanks, I did find that but to be honest, it made no sense to me at all :P
-Rob

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Creating and Saving a Screenshot
« Reply #6 on: January 09, 2013, 11:53:59 pm »
Thanks, I did find that but to be honest, it made no sense to me at all :P

:D sorry for my assumptions. for your problem, just add HDC is declared in unit LCLType just add it to your uses clause.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

RobWitcher

  • New Member
  • *
  • Posts: 31
Re: Creating and Saving a Screenshot
« Reply #7 on: January 10, 2013, 07:54:57 pm »
Thanks, I did find that but to be honest, it made no sense to me at all :P

:D sorry for my assumptions. for your problem, just add HDC is declared in unit LCLType just add it to your uses clause.


Thanks. I had to add LCLIntf to the uses as well and it now compiles fine. The only thing I was wondering is what happens next? The screenshot has not been saved to the clipboard and I have not noticed it being saved anywhere. Do you know how I could declare in the code a place for it to be saved?
-Rob

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Creating and Saving a Screenshot
« Reply #8 on: January 10, 2013, 08:09:53 pm »
the code takes a screen shot in a TBitmap object you need to use that to save it in to a file eg
Code: [Select]
  SaveBitmap.SaveToFile('C:\Screenshot.bmp');

to make it a complete procedure

Code: [Select]
procedure SaveScreenShot(const aFilename:string);
var
  ScreenDC: HDC;
  SaveBitmap: TBitmap;
begin
  SaveBitmap := TBitmap.Create;
  try
    SaveBitmap.SetSize(Screen.Width, Screen.Height);
    ScreenDC := GetDC(0);
    try
      SaveBitmap.LoadFromDevice(ScreenDC);
    finally
      ReleaseDC(0, ScreenDC);
    end;
    SaveBitmap.SaveToFile(aFilename);
  finally
    SaveBitmap.Free;
  end;
end;

just remember it will always be saved as a BMP file.
« Last Edit: January 10, 2013, 08:14:04 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

RobWitcher

  • New Member
  • *
  • Posts: 31
Re: Creating and Saving a Screenshot
« Reply #9 on: January 10, 2013, 11:39:54 pm »
Thanks a lot to taazz!

For anyone else having similar problems to me, here is my full code.

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, LCLType,
  LCLIntf, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    procedure SaveScreenShot(aFilename:string);
    procedure CreateComponents;
  end;

var
  Form1: TForm1;
  BMPFileName: String;

implementation

{$R *.lfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Edit1.text = '' then
  begin
    ShowMessage('You have to enter a filename in the edit box!');
  end
  else
  begin
    BMPFileName:=Edit1.text+'.bmp';
    SaveScreenShot(BMPFileName);
    ShowMessage('Screenshot successful!');
  end;
end;

procedure TForm1.CreateComponents;
begin
  Button1:=TButton.Create(Self);
  Edit1:=TEdit.Create(Self);
  with Button1 do
  begin
    Parent:=Self;
    caption:='Take Screenshot';
    autosize:=true;
    height:=25;
    left:=8;
    top:=8;
    OnClick:=@Button1Click;
  end;
  with Edit1 do
  begin
    Parent:=Self;
    text:='Filename';
    height:=23;
    width:=75;
    left:=8;
    top:=40;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  CreateComponents;
end;

procedure TForm1.SaveScreenShot(aFilename:string);
var
  ScreenDC: HDC;
  SaveBitmap: TBitmap;
begin
  SaveBitmap := TBitmap.Create;
  try
    SaveBitmap.SetSize(Screen.Width, Screen.Height);
    ScreenDC := GetDC(0);
    try
      SaveBitmap.LoadFromDevice(ScreenDC);
    finally
      ReleaseDC(0, ScreenDC);
    end;
    SaveBitmap.SaveToFile(aFilename);
  finally
    SaveBitmap.Free;
  end;
end;

end.

Also, in case you can't be bothered to copy and paste that, I will leave a link to a ZIP file containing my whole project.


http://www.mediafire.com/?wpga18ahpvpfx94
-Rob

 

TinyPortal © 2005-2018