When capturing screen the error occurs: raised exception class EOutOfResources with message

1

I'm using Embarcadero RAD Studio 2010 Delphi.

I was testing to capture screen images from other computers via sockets. So I came across an error in capturing the screen and turning to JPG (or even recording as default BMP), after about 10 seconds - running a TTimer with a range of 400 gives the error:

  

[projectname] raised exception class EOutOfResources with message   'Insufficient storage space to process this command.'.

When you see the Timer event code, you will see that (for tests) I'm just running function CapturaTelaJpg without saving it at all (but it saves perfectly), and even then the error occurs after 10 seconds. / p>

function CapturaTelaJpg: TJpegImage;
var
  dc : hdc;
  cv : TCanvas;
  aux : TBitmap;
begin

  Result := TJPEGImage.Create;

  aux := TBitmap.Create;
  aux.Height := Screen.Height;
  aux.Width := Screen.Width;

  dc := GetDC(0);
  cv := TCanvas.Create;
  cv.Handle := dc;

  //--Define o tamanho da imagem
  aux.Canvas.CopyRect(Rect(0,0,Screen.Width,Screen.Height),cv,
  Rect(0,0,Screen.Width,Screen.Height));
  cv.Free;
  cv := nil;
  ReleaseDC(0,dc);

  //-- Compacta o BMP para JPEG
  Result.Assign(aux);
  aux.Free;
  aux := nil;
  Result.Compress;

end;

Timer1 with interval 400

procedure TfrmMonitorando.Timer1Timer(Sender: TObject);
begin

  CapturaTelaJpg; // 10s depois, ocorre o erro colocando ou não numa variável

end;
    
asked by anonymous 02.01.2016 / 18:04

1 answer

2

Friend, create a new project, put use of function and declare in uses JPEG .

Now add a component TImage , a component TTimer with Interval 400.

In the TTimer component apply the function call but this way:

procedure frmTeste.Timer1Timer(Sender: TObject);
begin
  Image1.Picture.Assign(CapturaTelaJpg);
end;

It has run perfectly without any errors for more than 5 minutes!

The problem is that you called the function and did not use its contents!

  

Note, I did not modify anything, just copied its function and pasted it!   The only change was in the call!

Edit:

Come on, at the beginning of your function add:

//Aqui adicionamos uma tentativa de eliminar resto de memória!
  if Result <> nil then
    Result := Nil;

Comment or remove the line Result.Compress;

PS: Do not change anything else in your role! Rest is working Well!

    
03.01.2016 / 14:33