Solved

Cutting and saving piece of JPG-file...

Posted on 2002-04-01
6
265 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
[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
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
Industry Leaders: 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!

 

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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