Discussion:
Resizing Image
Farzan Behzadian
2002-09-04 22:41:43 UTC
Permalink
Hi members;

I want to resize an image. I mean having an image with different *Actual* dimensions, not resizing its look by changing Width and Height property.
For your information, I have a database that holds some Images in it and an application that makes a Photo Album of them. For increasing the speed of my application I want to store Thumbnails of those images in the database too. How can I make smaller image from the original ones?

Thank you in advance;
Farzan Behzadian.
Stephen Wood
2002-09-04 22:01:29 UTC
Permalink
Well the very easiest way is to dump the image into a TImage, set the
StretchDraw property to true, resize your image and save it again...

otherwise you can do it manually with a TCanvas.StretchDraw method...

Just remember to resize the image proportionately or else it won't look too
good...

HTH
Post by Farzan Behzadian
Hi members;
I want to resize an image. I mean having an image with different *Actual*
dimensions, not resizing its look by changing Width and Height property.
Post by Farzan Behzadian
For your information, I have a database that holds some Images in it and
an application that makes a Photo Album of them. For increasing the speed of
my application I want to store Thumbnails of those images in the database
too. How can I make smaller image from the original ones?
Post by Farzan Behzadian
Thank you in advance;
Farzan Behzadian.
farzan_beh
2002-09-04 23:50:19 UTC
Permalink
Thank you Stephen for your invaluable help; It worked properly.
But I didnt get about "resize the image proportionately". As you mentioned it
doesnt have a very good look. Would you explain following part more?
Post by Stephen Wood
Just remember to resize the image proportionately or else it won't look too
good...
Thank you ver much;
Farzan Behzadian.
Post by Stephen Wood
Well the very easiest way is to dump the image into a TImage, set the
StretchDraw property to true, resize your image and save it again...
otherwise you can do it manually with a TCanvas.StretchDraw method...
Just remember to resize the image proportionately or else it won't look too
good...
HTH
mike.gibbard
2002-09-05 08:33:36 UTC
Permalink
Hi Farzan,

Here's my ResizeJPG function that may help you. It takes a JPG image,
converts it to a Bitmap (to do the resizing) then converts it back to a JPG.
The resizing 'Scale' should be passed as a percentage of the original image
(e.g. 50 will halve the size of the original etc.). Note too, that this
function only reduces the size of the image. To increase the size we'd have
to do some super-sampling and the results are generally crap when you try to
increase an image.

function ResizeJPG(JPGName, JPGSaveAs: string; JPGQual: TJPEGQualityRange;
Scale: Double; AddOverlay: boolean; var NewSize: TNewSize): boolean;
var
jpg: TJPEGImage;
PBitmap: TBitmap;
ARect: TRect;
begin
Result := FALSE;

if not FileExists(JPGName) or
(Scale >= 1)or
(Scale <= 0) then
Exit;

// Convert JPG to BMP
PBitmap := TBitmap.Create;
try
jpg := TJPEGImage.Create;
try
jpg.LoadFromFile(JPGName);

with PBitmap do
begin
PixelFormat := pf24bit;

// Resize the BMP image
Height := Trunc(jpg.Height * Scale);
Width := Trunc(jpg.Width * Scale);
ARect.Left := 0;
ARect.Top := 0;
ARect.Right := Trunc((jpg.Width + 1) * Scale);
ARect.Bottom := Trunc((jpg.Height + 1) * Scale);
NewSize.Height := ARect.Bottom;
NewSize.Width := ARect.Right;

// Draw the JPG on the re-sized BMP image
Canvas.StretchDraw(ARect, jpg);
end;
finally
jpg.Free;
end;

// Convert the resized BMP back to a JPG and save it
jpg := TJPEGImage.Create;
try
jpg.CompressionQuality := JPGQual;
jpg.Assign(PBitmap);

jpg.SaveToFile(JPGSaveAs);
if AddOverlay then
begin
AddTextOverlay(JPGSaveAs,JPGSaveAs);
end;
Result := FileExists(JPGSaveAs);
finally
jpg.Free;
end;

finally
PBitmap.Free;
end;
end;

HTH

Mike

----- Original Message -----
From: "farzan_beh" <f-***@araku.ac.ir>
To: <delphi-***@yahoogroups.com>
Sent: Thursday, September 05, 2002 12:50 AM
Subject: Re: [Delphi] Resizing Image - Again
Post by farzan_beh
Thank you Stephen for your invaluable help; It worked properly.
But I didnt get about "resize the image proportionately". As you mentioned it
doesnt have a very good look. Would you explain following part more?
Post by Stephen Wood
Just remember to resize the image proportionately or else it won't look too
good...
Thank you ver much;
Farzan Behzadian.
Post by Stephen Wood
Well the very easiest way is to dump the image into a TImage, set the
StretchDraw property to true, resize your image and save it again...
otherwise you can do it manually with a TCanvas.StretchDraw method...
Just remember to resize the image proportionately or else it won't look too
good...
HTH
http://www.delphicollection.com/public/Digests/index.shtml
---------------------------------------------------------------
---------------------------------------------------------------
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
mike.gibbard
2002-09-05 08:42:03 UTC
Permalink
Ooops. Small mistake in the explanation. The Scale parameter should, of
course, be between 0 and 1 (e.g. .5 will halve the image size).

Mike

----- Original Message -----
From: "mike.gibbard" <***@0800dial.com>
To: <delphi-***@yahoogroups.com>
Sent: Thursday, September 05, 2002 9:33 AM
Subject: Re: [Delphi] Resizing Image - Again
Post by Jörn Zietz
Hi Farzan,
Here's my ResizeJPG function that may help you. It takes a JPG image,
converts it to a Bitmap (to do the resizing) then converts it back to a JPG.
The resizing 'Scale' should be passed as a percentage of the original image
(e.g. 50 will halve the size of the original etc.). Note too, that this
function only reduces the size of the image. To increase the size we'd have
to do some super-sampling and the results are generally crap when you try to
increase an image.
function ResizeJPG(JPGName, JPGSaveAs: string; JPGQual: TJPEGQualityRange;
Scale: Double; AddOverlay: boolean; var NewSize: TNewSize): boolean;
var
jpg: TJPEGImage;
PBitmap: TBitmap;
ARect: TRect;
begin
Result := FALSE;
if not FileExists(JPGName) or
(Scale >= 1)or
(Scale <= 0) then
Exit;
// Convert JPG to BMP
PBitmap := TBitmap.Create;
try
jpg := TJPEGImage.Create;
try
jpg.LoadFromFile(JPGName);
with PBitmap do
begin
PixelFormat := pf24bit;
// Resize the BMP image
Height := Trunc(jpg.Height * Scale);
Width := Trunc(jpg.Width * Scale);
ARect.Left := 0;
ARect.Top := 0;
ARect.Right := Trunc((jpg.Width + 1) * Scale);
ARect.Bottom := Trunc((jpg.Height + 1) * Scale);
NewSize.Height := ARect.Bottom;
NewSize.Width := ARect.Right;
// Draw the JPG on the re-sized BMP image
Canvas.StretchDraw(ARect, jpg);
end;
finally
jpg.Free;
end;
// Convert the resized BMP back to a JPG and save it
jpg := TJPEGImage.Create;
try
jpg.CompressionQuality := JPGQual;
jpg.Assign(PBitmap);
jpg.SaveToFile(JPGSaveAs);
if AddOverlay then
begin
AddTextOverlay(JPGSaveAs,JPGSaveAs);
end;
Result := FileExists(JPGSaveAs);
finally
jpg.Free;
end;
finally
PBitmap.Free;
end;
end;
HTH
Mike
----- Original Message -----
Sent: Thursday, September 05, 2002 12:50 AM
Subject: Re: [Delphi] Resizing Image - Again
Post by farzan_beh
Thank you Stephen for your invaluable help; It worked properly.
But I didnt get about "resize the image proportionately". As you
mentioned
Post by Jörn Zietz
it
Post by farzan_beh
doesnt have a very good look. Would you explain following part more?
Post by Stephen Wood
Just remember to resize the image proportionately or else it won't
look
Post by Jörn Zietz
too
Post by farzan_beh
Post by Stephen Wood
good...
Thank you ver much;
Farzan Behzadian.
Post by Stephen Wood
Well the very easiest way is to dump the image into a TImage, set the
StretchDraw property to true, resize your image and save it again...
otherwise you can do it manually with a TCanvas.StretchDraw method...
Just remember to resize the image proportionately or else it won't
look
Post by Jörn Zietz
too
Post by farzan_beh
Post by Stephen Wood
good...
HTH
http://www.delphicollection.com/public/Digests/index.shtml
---------------------------------------------------------------
---------------------------------------------------------------
Your use of Yahoo! Groups is subject to
http://docs.yahoo.com/info/terms/
Post by Jörn Zietz
http://www.delphicollection.com/public/Digests/index.shtml
---------------------------------------------------------------
---------------------------------------------------------------
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Godwin Stewart
2002-09-05 08:54:57 UTC
Permalink
On Thu, 5 Sep 2002 09:42:03 +0100, "mike.gibbard"
Post by mike.gibbard
Ooops. Small mistake in the explanation. The Scale parameter should, of
course, be between 0 and 1 (e.g. .5 will halve the image size).
Just remember that Windows has trouble shrinking images if the reduction
factor is greater than 1:8 or something like that. In my routines I make it
do the reduction in more than one step if the reduction factor is greater
than 1:6.
--
G. Stewart -- ***@sgms-centre.com
***@spamcop.net
Registered Linux user #284683

GnuPG key : BA3D01C6 (pgp.mit.edu)
Fingerprint: C3DF C686 6572 6E59 E3E4 0F40 2B9A 2218 BA3D 01C6
---------------------------------------------------------------
Sign spotted on a repair shop door:
WE CAN REPAIR ANYTHING.
(PLEASE KNOCK HARD ON THE DOOR - THE BELL DOESN'T WORK)
Jörn Zietz
2002-09-05 06:51:47 UTC
Permalink
Hi Farzan,
Post by Farzan Behzadian
I want to resize an image. I mean having an image with different
*Actual* dimensions, not resizing its look by changing Width and
Height property. For your information, I have a database that holds
some Images in it and an application that makes a Photo Album of
them. For increasing the speed of my application I want to store
Thumbnails of those images in the database too. How can I make
smaller image from the original ones?
Shrinking is a bit easier than stretching . When i did this i loaded
an image to an own class an wrote a methode to copy the pixel info to
another image object. To shrink the image i layed a grid over the
source image containing as many rows and cols as the target image
should have. The cells need not to be of same size (and i think that
is the normal case). E.g. if you have a source width of 10 pixels
(very low, only for example) and want to resize it to 4 pixels you
have 2 source cells of 2 pixels and 2 source cells of 3 pixels. To
get the target color you have to calc the average of the color values
per cell.

BTW there are many math ways to calculate an average. If the
source image is large and the target image is very small the "normal"
average (adding all values and dividing them by the count) results in
a very bright image. I cant recall the exact names of the other
methods but some are better.

Thats not the easiest way but you get the best results .
--
Cheers,
Joern
Stephen Wood
2002-09-05 08:06:32 UTC
Permalink
If you went the TImage route, and you have I think D5 upwards, there is also
a Proportional property...set that to true and you picture will scale
correctly...

Otherwise it's pretty simple...

Calculate your scale by taking the minimum scale that will convert your
picture into the thumbnail size...

AScale := Min (Thumbnail.Width / Picture.Width,Picture.Height /
Thumbnail.Height);

then you use that scale to calculate the rectangle to stretchdraw your
picture...

ARect := Rect (0,0,Picture.Width * AScale,Picture.Height * AScale);

HTH

-----Original Message-----
From: farzan_beh [mailto:f-***@araku.ac.ir]
Sent: 05 September 2002 01:50
To: delphi-***@yahoogroups.com
Subject: Re: [Delphi] Resizing Image - Again


Thank you Stephen for your invaluable help; It worked properly.
But I didnt get about "resize the image proportionately". As you mentioned
it
doesnt have a very good look. Would you explain following part more?
Post by Stephen Wood
Just remember to resize the image proportionately or else it won't look
too
Post by Stephen Wood
good...
Thank you ver much;
Farzan Behzadian.
Loading...