Solved

Cutting and saving piece of JPG-file...

Posted on 2002-04-01
6
255 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
Comment Utility
listeninig....
0
 
LVL 6

Expert Comment

by:DrDelphi
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Expert Comment

by:DelFreak
Comment Utility
Listening...
0
 

Author Comment

by:Dippen
Comment Utility
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
Comment Utility
Cheers.

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now