Solved

Cutting and saving piece of JPG-file...

Posted on 2002-04-01
6
262 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:Dippen
6 Comments
 
LVL 3

Expert Comment

by:vladh
ID: 6911295
listeninig....
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6911966
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);

0
 
LVL 6

Accepted Solution

by:
DrDelphi earned 300 total points
ID: 6911970
Opps. I missed the part where you wanted to save them to 50 x 50 pics. Here's the revised code:



var bmpToLoad,bmpToSave,smallBmp:Tbitmap;
    Jpg,smallJpg:TJpegimage;
begin
   Jpg:=TJpegimage.Create;
   BmpToLoad:=TBitmap.create;
   BmpToSave:=Tbitmap.create;
   SmallBmp:=Tbitmap.create;
   SmallJpg:=TJpegImage.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;

  With SmallBmp do
  begin
    height:=50;
    width:=50;
    Pixelformat:=pf24bit;
    Canvas.StretchDraw(Canvas.Cliprect,BmpToSave);
    SaveToFile('Z:\Smallpic.bmp');
  end;


   SmallJpg.assign(smallbmp);
   SmallJpg.SaveToFile('Z:\smallJpeg.jpg');
   Jpg.assign(BmpToSave);
   Jpg.SaveToFile('z:\MynewJpg.jpg');
   BmpToSave.SaveToFile('z:\mynewBmp.bmp');
   FreeAndNil(bmptoLoad);
   FreeAndNil(bmptoSave);
   FreeAndNil(Jpg);
   FreeAndNil(SmallJpg);
   FreeAndNil(SmallBmp);



end;




Good luck!!
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Expert Comment

by:DelFreak
ID: 6912145
Listening...
0
 

Author Comment

by:Dippen
ID: 6914086
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!
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6914111
Cheers.

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…

840 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