Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need to resize some jpg's, can i do it in BCB?

37 views
Skip to first unread message

Eric B.

unread,
Jul 26, 2002, 1:41:40 AM7/26/02
to
Hi,
We are scanning a bunch of our family pics for our website.
I need to resize a LOT of pictures (all jpg's) I have BCB5 and
am a reasonably competent programmer. What i want to know is:
Can i build a quick little program to resize a directory full of jpg's
such that the width is always reduced to 400 pixels and the aspect
ratio is maintained? I'm kind of looking for something like
JPG->LodFromFile
JPG->Resize
JPG->SaveToFile

I want to do this but not spend a lot of development time.
Is this possible with BCB?
Thanks
Eric


Paul Witheridge

unread,
Jul 26, 2002, 6:37:57 AM7/26/02
to
On Fri, 26 Jul 2002 05:41:40 GMT, "Eric B." <NoS...@NoSpammers.com>
wrote:

I have been involved in a very similar excercise and have found the
following useful:

1. A Delphi component (extract from ReadMe):

Component Name: TmThumbs
Author: Mats Asplund
Creation: 2002-01-25
Version: 1.0
Description: A component who produces jpeg-thumbnails.
Credit: Made from a Public Domain project by
Artchil Gogava, ArGo Software
E-mail: mas...@telia.com
Site: http://go.to/masdp

2. A nice standalone utility (extract from ReadMe):

Mihov Image Resizer 0.4
------------------------------------
Mihov Image Resizer is a handy tool
for resizing images in bmp, gif, and
jpg picture formats. It can also
convert images from and to bmp,
gif, and jpg.
Mihov Image Resizer is FREE.
----------------------------------
Miha Psenica, © 2000-2001
free...@mihov.com
http://www.mihov.com/eng/ir.html
http://www.mihov.com/update

Neither of the above does quite what I want so I am writing my own
using BCB5 (nowhere near complete yet as I keep side tracking) but I
did write a small test program to check out creation of thumbnails by:

a) Loading jpeg
b) converting to a bitmap (via Assign)
c) Using StretchDraw to draw this bitmap as a thumbnail on a the
canvas of a second bitmap
d) Saving the bitmap to file (not a jpeg in this test)

An important part I did not bother to include in the program (since it
was just a test) was setting Height/Width of the second bitmap (before
drawing on it) in order to preserve the aspect ratio.

Note that it is no good just saving the first bitmap created by step
(b) as this retains the original image size when saved to file.

The test program is basically as follows. Note the TImage controls
SourceImage and ThumbnailImage must have properties Autosize=false and
Stretch=true. Label1, 2 and 3 are just so I could easily check the
sizes of the graphics.

---------------------- main.h ---------------------------

class TMainForm : public TForm
{
__published: // IDE-managed Components
TGroupBox *ImageGroupBox;
TImage *SourceImage;
TGroupBox *SourceFileNameGroupBox;
TLabel *SourceFileLabel;
TOpenDialog *SourceFileOpenDialog;
TGroupBox *ThumbnailGroupBox;
TImage *ThumbnailImage;
TSaveDialog *ThumbnailSaveDialog;
TLabel *Label1;
TLabel *Label2;
TLabel *Label3;
void __fastcall SourceFileLabelClick(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TMainForm(TComponent* Owner);
};

---------------------- main.cpp -------------------------

void __fastcall TMainForm::SourceFileLabelClick(TObject *Sender)
{
if (SourceFileOpenDialog->Execute())
{
SourceFileLabel->Caption=SourceFileOpenDialog->FileName;
try
{

SourceImage->Picture->LoadFromFile(SourceFileOpenDialog->FileName);
}
catch ( Exception const& e )
{

Application->MessageBox(e.Message.c_str(),Application->Title.c_str(),0);
}

// Note need to adjust TImage Top, Left, Height, Width here to
// maintain aspect ratio and keep it centred in the parent control.


ThumbnailImage->Picture->Bitmap->Assign(SourceImage->Picture->Graphic);
Label1->Caption=AnsiString(ThumbnailImage->Left) +','
+AnsiString(ThumbnailImage->Width)+','
+AnsiString(ThumbnailImage->Top) +','
+AnsiString(ThumbnailImage->Height);
Label2->Caption=AnsiString(ThumbnailImage->Picture->Width)+','
+AnsiString(ThumbnailImage->Picture->Height);

Label3->Caption=AnsiString(ThumbnailImage->Picture->Bitmap->Width)+','

+AnsiString(ThumbnailImage->Picture->Bitmap->Height);
Graphics::TBitmap* p=new Graphics::TBitmap;
p->Height=ThumbnailImage->Height;
p->Width=ThumbnailImage->Width;
p->Canvas->StretchDraw(TRect(0,0,p->Width,p->Height),
ThumbnailImage->Picture->Bitmap);
if ( ThumbnailSaveDialog->Execute() )
p->SaveToFile(ThumbnailSaveDialog->FileName) ;
delete p ;
}
}

---------------------- form (as text) -------------------

object MainForm: TMainForm
Left = 200
Top = 107
Width = 577
Height = 566
Caption = 'Test creating thumbnail'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 384
Top = 232
Width = 32
Height = 13
Caption = 'Label1'
end
object Label2: TLabel
Left = 384
Top = 266
Width = 32
Height = 13
Caption = 'Label2'
end
object Label3: TLabel
Left = 384
Top = 299
Width = 32
Height = 13
Caption = 'Label3'
end
object ImageGroupBox: TGroupBox
Left = 8
Top = 8
Width = 353
Height = 473
Caption = 'Source Image'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = [fsBold]
ParentFont = False
TabOrder = 0
object SourceImage: TImage
Left = 12
Top = 17
Width = 329
Height = 449
Stretch = True
end
end
object ThumbnailGroupBox: TGroupBox
Left = 384
Top = 16
Width = 169
Height = 145
Caption = 'Thumbnail'
TabOrder = 1
object ThumbnailImage: TImage
Left = 8
Top = 16
Width = 153
Height = 121
Stretch = True
end
end
object SourceFileNameGroupBox: TGroupBox
Left = 8
Top = 488
Width = 353
Height = 41
Caption = 'Source File'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = [fsBold]
ParentFont = False
TabOrder = 2
object SourceFileLabel: TLabel
Left = 8
Top = 16
Width = 337
Height = 17
AutoSize = False
Layout = tlCenter
OnClick = SourceFileLabelClick
end
end
object SourceFileOpenDialog: TOpenDialog
Left = 392
Top = 176
end
object ThumbnailSaveDialog: TSaveDialog
DefaultExt = 'bmp'
Left = 456
Top = 176
end
end

Paul Witheridge - Marlow, UK

Eric B.

unread,
Jul 27, 2002, 12:05:36 AM7/27/02
to
Thank You, I will spend some time with the code you posted.
If i can add to it i'll send you my results.
Thanks
Eric

--
...
...
...This Post Has Been Tested and is Anthrax Free...
...
...

"Paul Witheridge" <paul.wi...@ntlworld.com> wrote in message
news:ne82kusm5q4qmerqm...@4ax.com...

Eric B.

unread,
Jul 27, 2002, 2:39:43 PM7/27/02
to
Here's some code i came up with.
The problem is that:
1. the result is a blank (all white) Image2
2. The line: Bitmap->Canvas->Draw(0,0,Image1->Picture->Bitmap);
causes the original Image1 to disappear off the form.
What am i missing here?
Thanks
Eric


TJPEGImage *JPeg1;
double H, W, NewWidth;
Graphics::TBitmap *Bitmap;

NewWidth = 300;

JPeg1 = new TJPEGImage();
JPeg1->LoadFromFile(Src);
H = JPeg1->Height;
W = JPeg1->Width;

Image1->AutoSize = false;
Image1->Stretch = true;
Image1->Width = NewWidth;
Image1->Height = H / (W / NewWidth);
Image1->Picture->Assign(JPeg1);

Bitmap = new Graphics::TBitmap;
Bitmap->Height = Image1->Height;
Bitmap->Width = Image1->Width;
Bitmap->Canvas->Draw(0,0,Image1->Picture->Bitmap);

Image2->AutoSize = true;
Image2->Visible = true;
Image2->Width = NewWidth;
Image2->Height = Image1->Height;
Image2->Picture->Assign(Bitmap);

delete Bitmap;
delete JPeg1;


Eric B.

unread,
Jul 27, 2002, 4:01:23 PM7/27/02
to
As I promised, if i figured it out I'd post it:
I wanted to be able to resize based on a new width so NewWidth is
specified and NewHeight is calculated. You can easily change that if you
want to.
Eric

bool __fastcall TForm1::ResizeJPG(AnsiString Src, AnsiString Dst, int
NewWidth)
{
TJPEGImage *JPeg1;
double H, W;
int NewHeight;
Graphics::TBitmap *Bitmap;

JPeg1 = new TJPEGImage();
JPeg1->LoadFromFile(Src);
H = JPeg1->Height;
W = JPeg1->Width;

NewHeight = H/(W/NewWidth);

Image1->AutoSize = false;
Image1->Stretch = true;
Image1->Width = NewWidth;

Image1->Height = NewHeight;
Image1->Picture->Assign(JPeg1);

Bitmap = new Graphics::TBitmap;
Bitmap->Height = Image1->Height;
Bitmap->Width = Image1->Width;

TRect rect = TRect(0,0,NewWidth, NewHeight);
Bitmap->Canvas->StretchDraw(rect,Image1->Picture->Graphic);

Image2->AutoSize = false;
Image2->Stretch = false;


Image2->Visible = true;
Image2->Width = NewWidth;

Image2->Height = NewHeight;
Image2->Canvas->Draw(0,0,Bitmap);
SaveBitMapAsJpeg(Image2->Picture->Bitmap, Dst);
delete Bitmap;
delete JPeg1;

}
//--------------------------------------------------------------------------
-

void __fastcall TForm1::SaveBitMapAsJpeg(Graphics::TBitmap *Bitmap,
AnsiString FileName)
{
TJPEGImage *jp = new TJPEGImage();
try
{
jp->Assign(Bitmap);
jp->SaveToFile(FileName);
}
__finally
{
delete jp;
}
}
//--------------------------------------------------------------------------
-

0 new messages