Re: Out of system resources



"Bjørge" <bjorge@xxxxxxx> schreef in bericht
news:fZqdnW2LjbETeMHYRVnzvA@xxxxxxxxxxxxxx

I believe you'll have to post a little more of your code...the
Thread.Execute; method, for instance.


I have to bring it down to essentials: JPG files only. TggThread is of
class(Tthread).
Results of some experimenting: a list of 60 large photo's (up to 1Mb) is
processed correctly, time and again. Another list of 10 small photo's
(<50kb) fails every now and then, but not always at the same photo.

type
TphotoRec = class
sourceFile : string;
targetFile : string;
targetWidth : integer;
targetHeight : integer;
end;

TphotoCopyThread= class(Tggthread)
sourceDir : string;
targetDir : string;
photoList : Tlist;
constructor Create(initiatorAddress : TggThread);
protected
procedure Execute; override;
end;


procedure CopyImageToBitmap(sourceFile : string; bitmap : Tbitmap; rect :
Trect);
// stretchdraw from source file to bitmap
var
picture : Tpicture;
begin
picture:=Tpicture.create;
try
try
Picture.LoadFromFile(sourceFile);
except
exit
end;

bitmap.Width:=rect.Right;
bitmap.Height:=rect.Bottom;
bitmap.Canvas.StretchDraw(rect,Picture.Graphic);
finally
picture.free
end;
end;

procedure CopyImageToSize(sourceFile,destinationFile : string; width,height
: integer);
// Read sourceFile and extract to bitmap in predetermined scale.
// Export to jpg.
var
ext : string;
bitmap : Tbitmap;
JPGimage : TjpegImage;
rect : Trect;
begin
bitmap:=Tbitmap.Create;

try
rect.Left:=0;
rect.Right:=width;
rect.Top:=0;
rect.Bottom:=height;

ext:=Uppercase(extractFileExt(sourceFile));
if (ext='.JPG') OR (ext='.JPEG') OR (ext='.BMP')
then CopyImageToBitmap(sourceFile,bitmap,rect)
else ; // skipped for this post

JPGimage:=TjpegImage.Create;
try
JPGimage.Assign(bitmap);
JPGimage.CompressionQuality:=60;
JPGimage.SaveToFile(destinationFile); // <------- if ERROR, then it
will happen here
except
end;
JPGimage.free

finally
bitmap.free
end;
end;

constructor TphotoCopyThread.Create(initiatorAddress: TggThread);
begin
inherited;
photoList:=Tlist.create;
end;

procedure TphotoCopyThread.Execute;
var
k : integer;
targetDir : string;
photo : TphotoRec;
begin
if photoList.Count=0
then exit;

targetDir:=ExtractFilePath(TphotoRec(photoList[0]).targetFile);
if NOT directoryExists(targetDir)
then CreateDir(targetDir);

for k:=0 to photoList.Count-1 do
begin photo:=TphotoRec(photoList[k]);
CopyImageToSize(photo.sourceFile,photo.targetFile,
photo.targetWidth,photo.targetHeight);
photo.Free;
end;

photoList.Free;
end;



.



Relevant Pages

  • Re: Importing Errors
    ... Dim strInputFileName As String ... Dim SourceFile, DestinationFile As String ...
    (microsoft.public.access.externaldata)
  • Excel VBA - Updating of data problem
    ... SourceAddress As String, PasteValuesOnly As Boolean, _ ... Dim SourceFile As String ... Set SourceWB = Workbooks.Open ... Set TargetRange = Range.Cells ...
    (microsoft.public.excel.programming)
  • RE: ADD HTML CODE
    ... SourceFile As String, _ ... Dim BullpenFile As String ... Open SourceFile For Input As F1 ...
    (microsoft.public.excel)
  • Importing Errors
    ... Dim strInputFileName As String ... Dim SourceFile, DestinationFile As String ...
    (microsoft.public.access.externaldata)
  • Excel VBA - Data Update
    ... SourceAddress As String, PasteValuesOnly As Boolean, _ ... Dim SourceFile As String ... Set SourceWB = Workbooks.Open ... Set TargetRange = Range.Cells ...
    (microsoft.public.excel.programming)