OutOfResources

I'll use this kind of code for loading thumbnails. Things going fine about 200 files and less that number of files. But above
that number of files i get this error message: The parameter is incorrect.

  public
    { Public declarations }
   Procedure LoadJpeg;
   Procedure imgOnDblClick(Sender: TObject);
  end;

var
 Img1Width : Integer;
  Img1Height : Integer;
  NewImage : TImage;
  NewShape : TShape;
  NewScrollBox : TScrollBox;
  CurrentFName : String;
  FNameList : TStringList;

Procedure TView.LoadJpeg;
 Var
  TheDir : String;
  FMask : String;
  res : word;
  WindFind : TWin32FindData;
  NextSearch : Boolean;
  imgW, imgH, imgL, imgT : Integer;
  imgCount : Integer;
  NewJpegImage : TJpegImage;
  imgJpegList : TStringList;
  NewBitMap : TBitmap;
  tmpLabel : TLabel;
Begin
 Screen.Cursor := crHourGlass;
 Shape1.Parent := Panel1;
 If Assigned(NewScrollBox) Then NewScrollBox.Free;
NewScrollBox := TScrollBox.Create(Panel1);
NewScrollBox.Parent := Panel1;
NewScrollBox.Align := alClient;
 tmpLabel := TLabel.Create(NewScrollBox);
 tmpLabel.Parent := NewScrollBox;
 tmpLabel.Caption := '     ';
 tmpLabel.Left := NewScrollBox.Width + 100;
Shape1.Parent := NewScrollBox;
  If Assigned(FNameList) Then FNameList.Free;
 FNameList := TStringList.Create;

  TheDir := DirView1.Path;
If TheDir[Length(TheDir)] <> '\' Then TheDir := TheDir + '\';
 FMask := FilterComboBox1.Mask;
imgW := 105;
imgH := NewScrollBox.Height - GetSystemMetrics(SM_CYHSCROLL) - 6;
imgL := 0;
imgT := 1;
imgJpegList := TStringList.Create;
  fillchar(WindFind,sizeof(TWin32FindData),0);
   NextSearch := True;
    res := Windows.FindFirstFile(PChar(TheDir + FMask), WindFind);
  While NextSearch Do
   Begin
    If Not ((StrPas(WindFind.cFileName) = '.') Or (StrPas(WindFind.cFileName) = '..')) Then
     If (WindFind.dwFileAttributes  And Not FILE_ATTRIBUTE_DIRECTORY) > 0 Then
     begin
      imgJpegList.Add(TheDir + StrPas(WindFind.cFileName));
     End;
     NextSearch := FindNextFile(res, WindFind);
    End;
     Windows.FindClose(res);
If imgJpegList.Count < 1 Then Exit;
  For imgCount := 0 To imgJpegList.Count - 1 Do
   Begin
    NewJpegImage := TJpegImage.Create;
    NewImage := TImage.Create(NewScrollBox);
    NewBitMap := TBitmap.Create;
    NewJpegImage.LoadFromFile(imgJpegList.Strings[imgCount]);
     FNameList.Add(imgJpegList.Strings[imgCount]);
    NewBitmap.Assign(NewJpegImage);
    NewImage.Visible := False;
    NewImage.Parent := NewScrollBox;
    NewImage.Picture.Assign(NewBitmap);
    NewImage.Left := imgL;
    NewImage.Top := imgT;
    NewImage.Width := imgW;
    NewImage.Height := imgH;
    NewImage.Stretch := True;
    imgL := imgL + imgW + 4;
    NewImage.Visible := True;
    Application.ProcessMessages;
    NewImage.OnDblClick := imgOnDblClick;
    NewBitmap.Free;
    NewJpegImage.Free;
   End;

    imgJpegList.Free;
    Screen.Cursor := crDefault;
    tmpLabel.Free;
End;

Procedure TView.imgOnDblClick(Sender: TObject);
Begin
 Shape1.Width := (Sender As TImage).Width + 4;
 Shape1.Height := (Sender As TImage).Height + 2;
 Shape1.Left := (Sender As TImage).Left - 2;
 Shape1.Top := (Sender As TImage).Top - 1;
 Shape1.Visible := True;
  ViewerForm.Image1.Picture := (Sender As TImage).Picture;
  ViewerForm.HorzScrollBar.Range := (Sender As TImage).Picture.Width;
  ViewerForm.VertScrollBar.Range := (Sender As TImage).Picture.Height;
  ViewerForm.Caption := 'Viewer 3.0' + ' - ' +
        Format(' (%d x %d)', [(Sender As TImage).Picture.Width, (Sender As TImage).Picture.Height]);
  ViewerForm.ClientWidth := (Sender As TImage).Picture.Width;
  ViewerForm.ClientHeight := (Sender As TImage).Picture.Height;
  ViewerForm.Show;
End;

procedure TView.SpeedButton8Click(Sender: TObject);
begin
Shape1.Visible := False;
  if FilterComboBox1.Mask = '*.JPG' then
  begin
  Panel1.Visible:= True;
  LoadJpeg;
  end;
end;

mhieta
LVL 2
mhietaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MadshiCommented:
Perhaps you should store the thumbnails in a TImageList instead of using several TImage components. You then would have to use e.g. a TPaintBox and draw the thumbnails at the right position in the TPaintBox.OnPaint event. With such a solution it doesn't matter, if you have 200 or 2000 thumbs...

Regards, Madshi.
0
mhietaAuthor Commented:
I have not used TImageList yet. What i have to change in above code?. Drawing to the TPaintBox.OnPaint event those thumbnails using TImageList.

Regards mhieta
0
MadshiCommented:
The TImage component does all for you. You don't need to do anything. But it has resource problems.
The TPaintBox component does nothing. It just calls you, when the content of the paint box needs repainting (and this is the OnPaint event). If you don't do anything in the OnPaint event handler, the paint box simply stays clean/empty/white.
So in TPaintBox.OnPaint you need to draw something. The best choice would be to load all your bitmaps like you do in the sources, but then don't assign the ready thumbnail to TImage.Bitmap, but instead add this bitmap to your image list. Then in OnPaint use the TImageList functions to draw the bitmaps at the right position.
You must do more than with TImage, your sources probably will get a bit longer, but the result is much faster and resource friendlier.

Regards, Madshi.

P.S: Sorry, have no time now to write sources for you...
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

brainwareCommented:
Even more Memory Freindly, a DIB and write it so simple and small as possiple, And why 200+ Thumbs at once?

if for generating pics "index pics" with 20x10 Thumbs etc or by showing it to user you could make it more Optimal by only generating and storing those that are visible to user, same way some Games work with large worlds or like 3D Studio Max uses the Material-Browser.

Here is list of Files "Images"
and we "play" that user only can see 3 Thumbs at a time :

aaaaaaa.jpg
bbbbbbb.jpg  (Not Visible but already Generated)
ccccccc.jpg  (Visible)
ddddddd.jpg  (Visible)
eeeeeee.jpg  (Visible)
fffffff.jpg  (Not Visible but already Generated)
ggggggg.jpg

Then you only show the c d and e pics and mabye have a and f ready too eg +xx and -xx pics so user can scroll a few realtime and same time u are already generating above and below ..
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mhietaAuthor Commented:
brainware,
Sometimes user has ex.200 or more or less jpg pics on the folder. So user can scroll those pics, when pics are generated. I know there are so many ways to do that, what i'm trying to do. But your succestion sounds nice also. Is there any sample or source code for that?.

Madshi,
i have no hurry to do that, what i'm trying to do.


Regards mhieta
0
brainwareCommented:
else there is Several of Thumb VCLS out there.. but my idea should not be needing a sample should it?

you just generate from File user Selected and xx forward and mabye a few backwards.. and display it..
0
mhietaAuthor Commented:
Hi, brainware

brainware, Ok i have this now done by you suggestion. And it runs perfectly. I modified that code what i showed in question. I think, it's not exactly what you suggested to me. So, it load's now 10 images forward and backwards. But that modification doesn't get rid off that resource problem.
Where i can find these Thumb VCLS?


Regards mhieta
0
mhietaAuthor Commented:
Hi!,

Ok, thanks brainware for your suggestion...

mhieta
0
mhietaAuthor Commented:
And also thanks to you Madshi!!, maybe next time you have some time...

Regards
mhieta
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.