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.
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.Heigh t;
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;
procedure ResizeImage(Image: TImage; DHeight: Double);
var
H: Integer;
W: Integer;
D: Double;
begin
D := 1;
H := Image.Picture.Bitmap.Heigh
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;
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.
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.
Why do You ignore my post. You've got there how to load image from database.
ASKER
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.
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.As sign(DBIma ge.Picture .Bitmap);
I := ThumbnailImage(Image[N], ThumbH);
Inc(N);
end;
It is just an idea....
emil
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.As
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.As sign(DBIma ge.Picture .Bitmap);
I := ThumbnailImage(Image[N], ThumbH);
Inc(N);
DataSet.Next;
end;
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.As
I := ThumbnailImage(Image[N], ThumbH);
Inc(N);
DataSet.Next;
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(TFo rmThumbnai ls, FormThumbnails);
Application.CreateForm(TFo rmTrueSize , FormTrueSize);
Application.CreateForm(TFo rmPic, 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.Heigh t;
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.Assig n(Picture. Bitmap);
Height := Image.Picture.Bitmap.Heigh t+40;
Width := Image.Picture.Bitmap.Width +16;
ShowModal;
end;
end;
destructor TeImage.Destroy;
begin
inherited Destroy;
end;
procedure TFormThumbnails.SpeedButto n1Click(Se nder: TObject);
var
RP: Word;
N: Word;
LP: Word;
TP: Word;
begin
RP := 0;
LP := Gap;
TP := Gap;
try
N := 0;
SetLength(Image, FormPic.TablePic.RecordCou nt);
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(Fo rmPic.DBIm age.Pictur e);
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.SpeedButto n2Click(Se nder: 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.LoadFromFil e(OpenPict ureDialog. FileName);
TablePic.Append;
TablePic.FieldByName('PIC' ).Assign(I mage1.Pict ure.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.LoadFromFil e(OpenPict ureDialog. FileName);
TablePic.Edit;
TablePic.FieldByName('PIC' ).Assign(I mage1.Pict ure.Bitmap );
TablePic.Post;
end;
end;
end.
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(TFo
Application.CreateForm(TFo
Application.CreateForm(TFo
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.Heigh
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.Assig
Height := Image.Picture.Bitmap.Heigh
Width := Image.Picture.Bitmap.Width
ShowModal;
end;
end;
destructor TeImage.Destroy;
begin
inherited Destroy;
end;
procedure TFormThumbnails.SpeedButto
var
RP: Word;
N: Word;
LP: Word;
TP: Word;
begin
RP := 0;
LP := Gap;
TP := Gap;
try
N := 0;
SetLength(Image, FormPic.TablePic.RecordCou
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(Fo
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.SpeedButto
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
begin
if OpenPictureDialog.Execute then
begin
Image1.Picture.LoadFromFil
TablePic.Append;
TablePic.FieldByName('PIC'
TablePic.Post;
end;
end;
procedure TFormPic.FormCreate(Sender
begin
TablePic.Active := False;
TablePic.Active := True;
end;
procedure TFormPic.SpeedButton2Click
begin
if OpenPictureDialog.Execute then
begin
Image1.Picture.LoadFromFil
TablePic.Edit;
TablePic.FieldByName('PIC'
TablePic.Post;
end;
end;
end.
Place Image1 on a form and set
Image1.Stretch := True;
// loading from database to thumbnail
var
MS: TMemoryStream;
begin
MS := TMemoryStream.Create;
try
SQLDataSet1Images.SaveToSt
Image1.Picture.Bitmap.Load
finally
MS.Free;
end;
end;
// switching from thumbnail to full and vice versa
procedure TForm1.Image1Click(Sender:
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;