Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Code Bug driving me crazy - Assistance needed

Posted on 2007-12-02
18
Medium Priority
?
234 Views
Last Modified: 2010-05-18
Hi Experts,

I have tried fixing this myself for many hours now and I just cannot get it to work!

I have 2 TImages...This code basically creates a screenshot of both images together and saves it.

I of the Images is the frame and the other changes in a loop with the contents of the FilelistBox.

All this works BUT it's not saving the images to their original filename it's skipping 1 and this all the
original filenames are wrong :o/

Can you guys please check the code and see if anyone can spot the problem please???

Hope you can help

ST3VO



procedure TForm1.sButton1Click(Sender: TObject);
var i: integer;
    bmp: TBitmap;
   jpg: TJPegImage;
   TheFileName: String;
begin
   for i:=0 to FileListBox1.Items.Count-1 do
 
 
begin
 
 
 
  Image2.Picture.LoadFromFile(FileListBox1.Items[i]);
   bmp := TBitmap.Create;
  jpg := TJPegImage.Create;
  bmp.Width := Image1.Width;
  bmp.height := Image1.height;
  bmp.PixelFormat := pf24bit;
  BitBlt(bmp.Canvas.Handle,0,0,bmp.Width,bmp.Height,Form1.Canvas.Handle,Image1.Left,Image1.Top,SRCCOPY);
  
  jpg.Assign(bmp);
  
  
  jpg.Compress;
 
 
 
TheFileName:=('C:\dwnld\new\'+ExtractFileName(FileListBox1.Items[i]));
jpg.SaveToFile(TheFileName);
 
 
 bmp.Free;
  jpg.Free;
 
  Application.ProcessMessages;
 
end;
 
....

Open in new window

0
Comment
Question by:ST3VO
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 7
18 Comments
 

Author Comment

by:ST3VO
ID: 20393173
A bit of extra information that might help:

If I select one by one whithout looping it works fine as when selecting the list item the TImage
is populated with the picture and it takes the screenshot and saves it fine.

But the I put the code in the loop filenames get all mixed!

Hope this helps!
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20393189
Hmm...

You say "All this works BUT it's not saving the images to their original filename it's skipping 1 and this all the original filenames are wrong :o/"
Can you explain that a bit more?
Also check your begin ... ends -- in your code sample pasted above you are missing an end (there is an end for the loop, but not for the procedure -- so in other words check that your loop is working properly)

Everything else looks fine.
Can you give an example of the filenames listed in your filelistbox.
Are the filnames like this:
C:\Program Files\Somewhere\Else\MyPicture.jpg
OR like this:
MyPicture.jpg
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20393200
put   Application.ProcessMessages;
after   Image2.Load
to force the image to be drawn on the form before you try to snapshot the form
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 25

Expert Comment

by:imitchie
ID: 20393203
i suspect you had img1 appearing in the file saved as img2, etc. all 1 off because of a missing application.processmessages
if that doesn't help, try Image2.invalidate as well
0
 

Author Comment

by:ST3VO
ID: 20393205
The filenames are like this: MyPicture.jpg

About the Begin...end....

That's my fault ...I cleaned up the comments and old code before pasting here...but It compiles fine!

The code needs to

Select each filename in the filelistbox so that the image is loaded and the screenshot is taken.
then save the screenshot

When I do it manually...

I click on the Filelistbox item....the image is loaded into the TImage and then I hit save and it saves
to the original filename BUT it doesn't do that inside the loop!


 
0
 

Author Comment

by:ST3VO
ID: 20393227
The First Image is saving as blank...but takes the name of the first image and then all image filenames are wrong :o/
0
 

Author Comment

by:ST3VO
ID: 20393256
imitchie: Didn't fix the problem :o/

0
 
LVL 25

Expert Comment

by:imitchie
ID: 20393262
st3vo, try my suggestion yet?
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20393268
okay, add all these after
  Image2.Picture.LoadFromFile(FileListBox1.Items[i]);

then if it works, take them out one at a time
  Image2.Invalidate();
  Form1.Invalidate();
  Application.ProcessMessages;
0
 

Author Comment

by:ST3VO
ID: 20393273
Yep...I still got the problem :o/

I'm sure it's something really simple that's happening there...I just cannot see it :o(
0
 

Author Comment

by:ST3VO
ID: 20393276
Here is a code update:
procedure TForm1.sButton1Click(Sender: TObject);
var i: integer;
    bmp: TBitmap;
   jpg: TJPegImage;
   TheFileName: String;
begin
   for i:=0 to FileListBox1.Items.Count-1 do
 
 
begin
 
 
  Image2.Picture.LoadFromFile(FileListBox1.Items[i]);
 
   bmp := TBitmap.Create;
  jpg := TJPegImage.Create;
  bmp.Width := Image1.Width;
  bmp.height := Image1.height;
  bmp.PixelFormat := pf24bit;
  BitBlt(bmp.Canvas.Handle,0,0,bmp.Width,bmp.Height,Form1.Canvas.Handle,Image1.Left,Image1.Top,SRCCOPY);
 
  jpg.Assign(bmp);
  jpg.CompressionQuality:=StrToInt(sComboBox1.Text);
  //jpg.Performance :=jpbestspeed;
  jpg.Compress;
 
 
 
TheFileName:=('C:\dwnld\new\'+ExtractFileName(FileListBox1.Items[i]));
jpg.SaveToFile(TheFileName);
 
 
 bmp.Free;
  jpg.Free;
 
  Application.ProcessMessages;
  Image2.invalidate
 
  end;
 
end;

Open in new window

0
 
LVL 25

Expert Comment

by:imitchie
ID: 20393290
You need to add the invalidate immediately after the Image2.Load, not that far down!
0
 

Author Comment

by:ST3VO
ID: 20393314
Still same problem :o(
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20393351
can you please list the current code again?
0
 

Author Comment

by:ST3VO
ID: 20393377
Sure:

Here it is!
procedure TForm1.sButton1Click(Sender: TObject);
var i: integer;
    bmp: TBitmap;
   jpg: TJPegImage;
   TheFileName: String;
begin
 
   for i:=0 to FileListBox1.Items.Count-1 do
 
 
begin
 
   Image2.Picture.LoadFromFile(FileListBox1.Items[i]);
   Image2.invalidate;
 
 
 
 
   bmp := TBitmap.Create;
  jpg := TJPegImage.Create;
  bmp.Width := Image1.Width;
  bmp.height := Image1.height;
  bmp.PixelFormat := pf24bit;
  BitBlt(bmp.Canvas.Handle,0,0,bmp.Width,bmp.Height,Form1.Canvas.Handle,Image1.Left,Image1.Top,SRCCOPY);
 
  jpg.Assign(bmp);
  jpg.CompressionQuality:=StrToInt(sComboBox1.Text);
  //jpg.Performance :=jpbestspeed;
  jpg.Compress;
 
 
 
TheFileName:=('C:\dwnld\new\'+(FileListBox1.Items[i]));
 
jpg.SaveToFile(TheFileName);
 
 
 bmp.Free;
  jpg.Free;
 
 
  Application.ProcessMessages;
 
 
  end;
 
 
 
end;

Open in new window

0
 
LVL 25

Accepted Solution

by:
imitchie earned 2000 total points
ID: 20393388
okay, please try this
procedure TForm1.sButton1Click(Sender: TObject);
var i: integer;
  bmp: TBitmap;
  jpg: TJPegImage;
  TheFileName: String;
begin
  for i:=0 to FileListBox1.Items.Count-1 do
  begin
    Image2.Picture.LoadFromFile(FileListBox1.Items[i]);
    Image2.invalidate;
    form1.Invalidate;
    Application.ProcessMessages;
 
   bmp := TBitmap.Create;
  jpg := TJPegImage.Create;
  bmp.Width := Image1.Width;
  bmp.height := Image1.height;
  bmp.PixelFormat := pf24bit;
  BitBlt(bmp.Canvas.Handle,0,0,bmp.Width,bmp.Height,Form1.Canvas.Handle,Image1.Left,Image1.Top,SRCCOPY);
 
  jpg.Assign(bmp);
  jpg.CompressionQuality:=StrToInt(sComboBox1.Text);
  //jpg.Performance :=jpbestspeed;
  jpg.Compress;
 
 
 
TheFileName:=('C:\dwnld\new\'+(FileListBox1.Items[i]));
 
jpg.SaveToFile(TheFileName);
 
 
 bmp.Free;
  jpg.Free;
 
 
  Application.ProcessMessages;
 
 
  end;
 
 
 
end;

Open in new window

0
 

Author Comment

by:ST3VO
ID: 20393392
YESSSSS!!!!

It works perfectly now!!!!!

Thanks a million :o)

I can finally go to sleep ...hehehehe!!!!!

Thanks again!!!
0
 

Author Closing Comment

by:ST3VO
ID: 31412246
Perfect!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question