多线程 – Delphi EOutOfResources屏幕捕获

给出此错误的程序.有时立即,有时在短时间后

http://www1.datafilehost.com/d/39f524c0线程暂停在一些尝试finally块

资源:

http://www1.datafilehost.com/d/1cae7b24 EOufOfResources在调试期间

我很抱歉英语不好.我有以下问题:我尝试做5 fps截图并在其上绘制光标图标,在PNG中重新编码BMP并通过阻塞套接字Indy通过网络发送.发送按比例压缩的屏幕截图并放置在主窗体上的TImage(desktopimage)之后.如果我在计时器中完成所有这些 – 如果我在Synchronize()中执行所有这些代码,一切正常,它也可以正常工作,但它会导致界面冻结,我想摆脱它,并且正在做所以在线程中的PNG压缩,现在我试图打破几个Synchronize()来找到错误(我得到一个错误EOutOfResources),但我不能.请帮忙.这是我的代码:

  TCaptureThread = class(TThread)
  private
   bmp: TBitmap;
   DC: HDC;
   h:hwnd;
   thumbRect : TRect;
   maxWidth, maxHeight:integer;
   png:TPNGImage;
   Stream:TMemoryStream;
   RecBlock:TCommBlock;
   r: TRect;
   CI: TCursorInfo;
   Icon: TIcon;
   II: TIconInfo;
   commblock:TCommblock;
   procedure showthumb;
   procedure send;
   procedure stretch;
   procedure getscreen;
   procedure fixsize;
  protected
   procedure Execute; override;
   constructor Create(CreateSuspended: Boolean);
   destructor destroy; override;
end;

 constructor TCaptureThread.Create(CreateSuspended: Boolean);
 begin
  bmp:=TBitmap.Create;
  Stream:=TMemoryStream.Create;
  png:=TPNGImage.Create;
  Icon := TIcon.Create;
  inherited Create(CreateSuspended);
 end;


 destructor TCaptureThread.destroy;
 begin
  png.Free;
  bmp.Free;
  Icon.Free;
  stream.Free;
  inherited;
 end;

 procedure TCaptureThread.Execute;
 begin
  inherited;
  while not Terminated do
  begin
   Synchronize(fixsize);
   Synchronize(getscreen);
   r := bmp.Canvas.ClipRect;
  try
   CI.cbSize := SizeOf(CI);
   if GetCursorInfo(CI) then
   if CI.Flags = CURSOR_SHOWING then
   begin
    Icon.Handle := CopyIcon(CI.hCursor);
    if GetIconInfo(Icon.Handle, II) then
    begin
      bmp.Canvas.Draw(
            ci.ptScreenPos.x - Integer(II.xHotspot) - r.Left - Form4.Left,
            ci.ptScreenPos.y - Integer(II.yHotspot) - r.Top - Form4.Top,
            Icon
            );
    end;
   end;
  finally

  end;
  try
   png.Assign(bmp);
   png.CompressionLevel := 9;
   png.SaveToStream(stream);
   stream.Position :=0;
   Recblock.Command :='STREAM';
   Recblock.Msg :='';
   Recblock.NameFrom := MyName;
   Synchronize(send);
  finally

  end;
  try
   thumbRect.Left := 0;
   thumbRect.Top := 0;
   if bmp.Width > bmp.Height then
   begin
    thumbRect.Right := maxWidth;
    thumbRect.Bottom := (maxWidth * bmp.Height) div bmp.Width;
   end
   else
   begin
    thumbRect.Bottom := maxHeight;
    thumbRect.Right := (maxHeight * bmp.Width) div bmp.Height;
   end;
   Synchronize(stretch);
   bmp.Width := thumbRect.Right;
   bmp.Height := thumbRect.Bottom;
   Synchronize(showthumb);
  finally
  end;

  sleep(200);
  end;

  end;

  procedure TCaptureThread.getscreen;
  begin
   DC:=GetDC(0);
   bitblt(bmp.Canvas.Handle, 0, 0, Form4.Width+Form4.Left, Form4.Height+Form4.Top,         
   DC, Form4.Left, Form4.Top, SRCCOPY);
   ReleaseDC(0, DC);
  end;

  procedure TCaptureThread.fixsize;
  begin
   maxWidth := Form1.DesktopImage.Width;
   maxHeight := Form1.DesktopImage.Height;
   bmp.Height:=Form4.Height;
   bmp.Width:=Form4.Width;
  end;

  procedure TCaptureThread.send;
  begin
   Form1.Streamclient.IOHandler.Write(RawToBytes(Recblock,sizeof(recblock)),sizeof(recblock));
   Form1.Streamclient.IOHandler.Write(stream,stream.Size,true);
  end;

  procedure TCaptureThread.showthumb;
  begin
   Form1.DesktopImage.Picture.Assign(bmp);
  end;

  procedure TCaptureThread.stretch;
  begin
   SetStretchBltMode(bmp.Canvas.Handle, HALFTONE);  
   StretchBlt(bmp.Canvas.Handle,0,0,thumbRect.Right,thumbRect.Bottom,bmp.Canvas.Handle,0,0,bmp.Width,bmp.Height,SRCCOPY);
  end;

最佳答案 首先在我的delphi 2010中我必须更换

unit CaptureUnit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

unit CaptureUnit;

interface

uses
  Windows, Messages, SysUtils, Variants,
  Classes, Graphics, Controls, Forms, Dialogs;

在unit.pas中也是如此

您不应该将位图分配给Picture.Assign(bmp);

procedure TCaptureThread.showthumb;
begin
    CaptureForm.DesktopImage.Picture.Assign(bmp);
end;

一段时间后,我也得到一个错误EOutOfResources).

你应该为Bit.Bitmap.Assign(bmp)分配一个位图;

procedure TCaptureThread.showthumb;
begin
    CaptureForm.DesktopImage.Picture.Bitmap.Assign(bmp);
end;

在我改变之后,我让你的程序运行了20分钟而没有出现错误.然后我手动完成了.

更新:

屏幕截图:程序运行时Vcl视频播放和拉伸以及移动捕获区域.

希望它能帮到你.

点赞