delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


How to create a thumbnail from a JPEG imageFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
Graphic
Skill Level:
Scoring:
Last Update:
02/02/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet JPEG jpg thumbnail image graphic timage tjpeg
Times Scored:
7
Visits:
5753
Uploader: Rafael Cotta
Company:
Reference: http://www.lci.ufrj.br/~rafaelf/
 
Question/Problem/Abstract:
Once I was trying to resize a jpeg image and made some Internet search. Believe or not, I couldn't find clear answers to my question, but it's very easy to do.

The code below will reduce width and height of a chosen .jpg image.

Go to "File / New / Console Application" and paste this code. Set the SizePct (a const on the code below, but can be a variable on your program) to fit your needs. If you want a new image with 30% of the original width and height set this to 30.

All I do is load the JPEG on a TJPEGImage, create a bitmap and .StretchDraw the JPEG on the bitmap. Then I copy the bitmap to a TJPEGImage using the .Assign method, and, finally, save it.

Enjoy!
Answer:



// ----- Code Starts Here -----

program Project1;

{$APPTYPE CONSOLE}

uses
  Classes, Windows, SysUtils, Dialogs, JPEG, Graphics;

const
        SizePct : integer = 50; { The new image will have 50% of the original }

var
        OpenDlg : TOpenDialog;
        SaveDlg : TSaveDialog;
        oJPG    : TJPEGImage;
        oBmp    : TBitmap;


begin
        OpenDlg := TOpenDialog.Create(nil);
        SaveDlg := TSaveDialog.Create(nil);

        if (OpenDlg.Execute) then
        begin
                try
                  begin
                        oJPG := TJPEGImage.Create;
                        oJPG.LoadFromFile(OpenDlg.FileName);
                  end
                except
                        MessageBox(
                                0,
                                PChar('Error while trying to open ' +
                                OpenDlg.FileName +
                                '.'),
                                PChar('Error'),
                                MB_OK or MB_ICONERROR
                        );
                        exit;
                end;

                oBmp := TBitmap.Create;
                oBmp.Width := Round(oJPG.Width * SizePct / 100);
                oBmp.Height := Round(oJPG.Height * SizePct / 100);
                oBmp.Canvas.StretchDraw(
                        Rect(0, 0, oBmp.Width - 1, oBmp.Height - 1),
                        oJPG
                );

                oJPG.Assign(oBmp);
                oJPG.Compress;

                if (SaveDlg.Execute) then
                begin
                        oJPG.SaveToFile(SaveDlg.FileName);
                end;

                oBmp.Free;
                oJPG.Free;
        end;

        OpenDlg.Free;
        SaveDlg.Free;

end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
JPEG
    João Vitor (Aug 15 2004 9:19AM)

Valeu mano.... c n tem noção d como caiu como uma luva pra mim esse código.... muito bom.... vlw mesmo......
Respond

RE: JPEG
v incentd (Feb 18 2005 4:36AM)

Fantastic m8,  
b4 this i could only resize a BMP file using the code:


Var
  Bmp: THandle;            
  Bmpinfo: Windows.TBitmap;  
  DC: HDC;  
  Openpic : TOpenDialog;
begin

Openpic := TOpenDialog.Create(nil);
if openpic.Execute = true then
begin

DC := CreateCompatibleDC(0);

bmp := LoadImage(0,PChar(openpic.FileName),IMAGE_BITMAP,0,0,
                         LR_LOADFROMFILE);

GetObject(BMP, SizeOf(Windows.TBitmap), @BitmapInfo);
SelectObject(DC, TheBitmap);

BitBlt (image1.Canvas.Handle,0,0,COVER.Width,Image1.Height,DC, 0,0,SRCCOPY);

StretchBlt(Image2.Canvas.Handle,0,0,Image2.Width,Image2.Height,DC,0,0,BitmapInfo.bmWidth,BmpInfo.bmHeight,SRCCOPY) ;

  image1.Refresh;
  image2.Refresh;
  ICON.Picture.SaveToFile(OPENPIC.GetNamePath + 'icon.bmp');

  DeleteDC(DC);
  DeleteObject(Bmp);




U make my life a lot easier now

thx again
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
C.A. Longen
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)