Главная страница | назад





Article #20164: Creating Thumbnails for Images Using TImage

QUESTION: I'm trying to create a thumbnail of a .jpg image on my form for a viewer program. Is there a quick way to accomplish this?

ANSWER: You can use TCanvas::StretchDraw().

This sample uses two TImage components, an OpenPictureDialog, and a TButton.

Drop the two TImage components on the form and name the first imgMain and set its dimensions to 400x400(Height x Width). Name the second TImage imgThumb and set its dimensions to 100x100. Then drop a TButton and the OpenPictureDialog component on the form and name them btnOpen and OpenPictureDialog respectively.

Define btnOpen OnCLick event:

void __fastcall TForm1::btnOpenClick(TObject *Sender)
{
OpenPictureDialog->Filter = "JPEG Image File (*.jpg)|*.jpg";
if(OpenPictureDialog->Execute())
{
TJPEGImage *jpg = new TJPEGImage();
try
{
jpg->LoadFromFile(OpenPictureDialog->FileName);
imgMain->Picture->LoadFromFile(OpenPictureDialog->FileName);
imgThumb->Canvas->StretchDraw(Rect(0,0,imgThumb->Width,imgThumb->Height),jpg);
}
catch(...)
{
ShowMessage("Unable to load: " + OpenPictureDialog->FileName);
}
delete jpg;
}
}

Last Modified: 10-MAY-00