Link to home
Start Free TrialLog in
Avatar of johnshailes
johnshailes

asked on

Thumbnail to Full Size

I have a picture database. I wish to reduce the picture's to show as a thubmnail on a form and by clicking on that thumbnail create a full size picture.
Avatar of mokule
mokule
Flag of Poland image

If images are bmp and equal in size You can do it easy.
Place Image1 on a form and set

Image1.Stretch := True;
// loading from database to thumbnail

var
  MS: TMemoryStream;
begin
  MS := TMemoryStream.Create;
  try
    SQLDataSet1Images.SaveToStream(MS);
    Image1.Picture.Bitmap.LoadFromStream(MS);
  finally
    MS.Free;
  end;
end;

// switching from thumbnail to full and vice versa
procedure TForm1.Image1Click(Sender: TObject);
begin
  if Image1.Stretch then
    begin
    Image1.Stretch := False;
    Image1.AutoSize := True;
    end
  else
    begin
    Image1.Stretch := True;
    Image1.AutoSize := False;
    Image1.Width := 100;
    Image1.Height := 100;
    end
end;
      
For a thubmnail you will use TImage components with equal height. The witdh of every picture will be calculated to save true proportion of the image. It is easy to develop OnClick event to show the picture by another form in true size....
For thubmnail:

procedure ResizeImage(Image: TImage; DHeight:  Double);
var
  H:      Integer;
  W:      Integer;
  D:      Double;
begin
  D := 1;
  H := Image.Picture.Bitmap.Height;
  W := Image.Picture.Bitmap.Width;
  if (H<>DHeight) then
  try
    begin
      Image.Stretch := True;
      D := DHeight/H
    end;
  finally
    Image.Height := Round(D*H);
    Image.Width := Round(D*W);
  end;
end;
download from        http://www.geocities.com/esoftbg/
                 file        Q_20951029.zip
it is a simple example.
Avatar of johnshailes
johnshailes

ASKER

esoftbg

Your sample is great. But how would i load a file from a .db file, because in my database the photo's relate to an index which in turn relate to a drawing number and operation number criteria.

john.
Hi
Why do You ignore my post. You've got there how to load image from database.
mokule

I appologise, I missed the SQLDataSet i was to engrosed in the example from esoftbg. I have been useing a TDBimage on my form so it made it easy to show BitMap. I will try all example's.

john.
johnshailes,
In my example I have declared  a static array:
    Image:  array [FrstImg..LastImg] of TeImage;
I think it would be in your case used a dynamic array:
    Image:  array of TeImage;
Then:
//......................................
  N := 0;
  SetLength(Image, DataSet.RecordCount); // Set the size of the array
  while not DataSet.EOF do
  begin
    Image[N] := TeImage.Create(Self);
    Image[N].Parent := ScrollBox;
    Image[N].Left := // Calculate Left position of every Image;
    Image[N].Top := // Calculate Top position of every Image;
    Image[N].Picture.Bitmap.Assign(DBImage.Picture.Bitmap);
    I := ThumbnailImage(Image[N], ThumbH);
    Inc(N);
  end;

It is just an idea....
emil
I forgot some important things:

  N := 0;
  SetLength(Image, DataSet.RecordCount); // Set the size of the array
  DataSet.First;
  while not DataSet.EOF do
  begin
    Image[N] := TeImage.Create(Self);
    Image[N].Parent := ScrollBox;
    Image[N].Left := // Calculate Left position of every Image;
    Image[N].Top := // Calculate Top position of every Image;
    Image[N].Picture.Bitmap.Assign(DBImage.Picture.Bitmap);
    I := ThumbnailImage(Image[N], ThumbH);
    Inc(N);
    DataSet.Next;
  end;
ASKER CERTIFIED SOLUTION
Avatar of esoftbg
esoftbg
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is not a new answer about this question. This is accepted code from the Author of the question. Let it be here, if my site would be destroyed for any reason.

program Q_20951029;

uses
  Forms,
  Unit_Q_20951029 in 'Unit_Q_20951029.pas' {FormThumbnails},
  Unit2_Q_20951029 in 'Unit2_Q_20951029.pas' {FormTrueSize},
  Unit3_Q_20951029 in 'Unit3_Q_20951029.pas' {FormPic};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TFormThumbnails, FormThumbnails);
  Application.CreateForm(TFormTrueSize, FormTrueSize);
  Application.CreateForm(TFormPic, FormPic);
  Application.Run;
end.

//..............................

unit Unit_Q_20951029;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, JPEG, DB, DBTables, Grids, DBGrids, ExtDlgs, Buttons,
  ToolWin, ComCtrls, Clipbrd;

const
  Gap = 16;
  ThumbH  = 64;
type
  TeImage = class(TImage)
    private    { Private declarations }
    protected  { Protected declarations }
      procedure  MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
    public     { Public declarations }
     constructor Create(AOwner: TComponent); override;
     destructor  Destroy; override;
    published  { Published declarations }
  end;

  TFormThumbnails = class(TForm)
    ScrollBox: TScrollBox;
    ToolBar1: TToolBar;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
  private  { Private declarations }
  public   { Public declarations }
    Image:  array of TeImage;
  end;

var
  FormThumbnails: TFormThumbnails;

implementation

{$R *.dfm}

uses
  Unit2_Q_20951029, Unit3_Q_20951029;

function  ThumbnailImage(Image: TeImage; DHeight:  Double): Integer;
var
  H:      Integer;
  W:      Integer;
  D:      Double;
begin
  try
    D := 1;
    H := Image.Picture.Bitmap.Height;
    W := Image.Picture.Bitmap.Width;
    if (H<>DHeight) then
    try
      begin
        Image.Stretch := True;
        D := DHeight/H
      end;
    finally
      Image.Height := Round(D*H);
      Image.Width := Round(D*W);
    end;
  finally
    Result := Image.Left + Image.Width;
  end;
end;

constructor TeImage.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
end;

procedure TeImage.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  with FormTrueSize do
  begin
    Image.Picture.Bitmap.Assign(Picture.Bitmap);
    Height := Image.Picture.Bitmap.Height+40;
    Width := Image.Picture.Bitmap.Width+16;
    ShowModal;
  end;
end;

destructor  TeImage.Destroy;
begin
  inherited Destroy;
end;

procedure TFormThumbnails.SpeedButton1Click(Sender: TObject);
var
  RP:     Word;
  N:      Word;
  LP:     Word;
  TP:     Word;
begin
  RP := 0;
  LP := Gap;
  TP := Gap;
  try
    N := 0;
    SetLength(Image, FormPic.TablePic.RecordCount);
    FormPic.TablePic.First;
    while not FormPic.TablePic.EOF do
    begin
      Image[N] := TeImage.Create(Self);
      if (N=0) then
      begin
        Image[N].Left := LP;
        Image[N].Top := TP;
      end;
      Image[N].Parent := ScrollBox;
      Image[N].Picture.Assign(FormPic.DBImage.Picture);
      if (RP>ScrollBox.Width) then
      begin
        TP := TP + ThumbH + Gap;
        RP := 0;
      end;
      Image[N].Left := RP + LP;
      Image[N].Top := TP;
      RP := ThumbnailImage(Image[N], ThumbH);
      Inc(N);
      FormPic.TablePic.Next;
    end;
  except
  end;
end;

procedure TFormThumbnails.SpeedButton2Click(Sender: TObject);
begin
  FormPic.ShowModal;
end;

end.

//...........................

unit Unit2_Q_20951029;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls;

type
  TFormTrueSize = class(TForm)
    Scroll_Box: TScrollBox;
    Image: TImage;
  private  { Private declarations }
  public   { Public declarations }
  end;

var
  FormTrueSize: TFormTrueSize;

implementation

{$R *.dfm}

end.

//........................

unit Unit3_Q_20951029;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, ExtDlgs, DB, DBTables, Buttons, ExtCtrls,
  JPEG, DBCtrls;

type
  TFormPic = class(TForm)
    DataSourcePic: TDataSource;
    OpenPictureDialog: TOpenPictureDialog;
    DBGridPic: TDBGrid;
    SpeedButton1: TSpeedButton;
    Image1: TImage;
    TablePic: TTable;
    SpeedButton2: TSpeedButton;
    DBImage: TDBImage;
    procedure SpeedButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
  private  { Private declarations }
  public   { Public declarations }
  end;

var
  FormPic: TFormPic;

implementation

{$R *.dfm}

procedure TFormPic.SpeedButton1Click(Sender: TObject);
begin
  if OpenPictureDialog.Execute then
  begin
    Image1.Picture.LoadFromFile(OpenPictureDialog.FileName);
    TablePic.Append;
    TablePic.FieldByName('PIC').Assign(Image1.Picture.Bitmap);
    TablePic.Post;
  end;
end;

procedure TFormPic.FormCreate(Sender: TObject);
begin
  TablePic.Active := False;
  TablePic.Active := True;
end;

procedure TFormPic.SpeedButton2Click(Sender: TObject);
begin
  if OpenPictureDialog.Execute then
  begin
    Image1.Picture.LoadFromFile(OpenPictureDialog.FileName);
    TablePic.Edit;
    TablePic.FieldByName('PIC').Assign(Image1.Picture.Bitmap);
    TablePic.Post;
  end;
end;

end.