Solved

Cutting and saving piece of JPG-file...

Posted on 2002-04-01
6
263 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
Independent Software Vendors: 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Process Javascript errors with Delphi TWebBrowser 1 203
Firemonkey webbrowser scrollbars ? 1 57
RESTRequest Parameter 4 53
enhance the following code 3 42
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 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