Link to home
Start Free TrialLog in
Avatar of Dippen
Dippen

asked on

Cutting and saving piece of JPG-file...

Hi dear experts!

I have a JPEG-image stored on disk (MyPic.jpg). Its size is 400x300 pixels, and the color-depth is 24 bit.
Now I would like to cut out the region (100,100,199,199) and save it both as a BMP (MyBmp.bmp) and a JPG (MyJPG.JPG). Now, the thing is that I want the two new pictures to be only 50x50 pixels in size. How do I do this?

Actually, I've managed to do it, but when using CopyRect the colors get terribly degraded and I don't know why. Could someone please give me full source for this problem, including creating and destroying of all components needed in the transformation?

Thanks!

/Daniel, Sweden
Avatar of vladh
vladh
Flag of Canada image

listeninig....
Avatar of DrDelphi
DrDelphi

var bmpToLoad,bmpToSave:Tbitmap;
    Jpg:TJpegimage;
begin
   Jpg:=TJpegimage.Create;
   BmpToLoad:=TBitmap.create;
   BmpToSave:=Tbitmap.create;
   With Jpg do
   begin
      PixelFormat:=jf24bit;
      LoadFromFile('z:\Myjpg.jpg');
   end;
   with bmpToLoad do
   begin
      PixelFormat:=pf24bit;
      Assign(Jpg);
   end;
   With bmpToSave do
   begin
     height:=199;
     Width:=100;
     PixelFormat:=pf24bit;
     Canvas.CopyRect(Canvas.Cliprect,bmpToLoad.canvas,Canvas.Cliprect);
   end;
   Jpg.assign(BmpToSave);
   Jpg.SaveToFile('z:\MynewJpg.jpg');
   BmpToSave.SaveToFile('z:\mynewBmp.bmp');
   FreeAndNil(bmptoLoad);
   FreeAndNil(bmptoSave);
   FreeAndNil(Jpg);

ASKER CERTIFIED SOLUTION
Avatar of DrDelphi
DrDelphi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Listening...
Avatar of Dippen

ASKER

Great work Doc! Thx alot!
The secret seems to be never to use CopyRect to resize the portion of the picture, but let StretchDraw do that work instead.

Points well earned!
Cheers.