home *** CD-ROM | disk | FTP | other *** search
- { Project Store.DPR Delphi 2.0 Demos
-
- Description:- Store.Dpr Project:-
-
- Demonstrates the use of:
-
- 1) 'FileName'
- 2) 'ImageFormat'
- 3) 'ImageCompression'
- 4) 'Write Options'
- 5) JPG
-
-
- Date of Origin: 18/04/96
- Original Author: Andrew Hutchison
- Modification History:
-
- Date Person Change
- ----------------------------------------------------
- 18/04/96 A Hutchison Created
-
- (c) Copyright Media Architects Inc. 1996.
- All rights reserved. No part of this program may be
- photocopied, reproduced, translated to another programming
- language or transported to any computer system without the
- prior written consent of Media Architects Inc.}
-
- unit UStore;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- OleCtrls, ImageKnife32, ExtCtrls, Menus, StdCtrls, Spin;
-
- type
- TForm1 = class(TForm)
- OpenDialog: TOpenDialog;
- MainMenu1: TMainMenu;
- File1: TMenuItem;
- LoadImage1: TMenuItem;
- SaveImageAs1: TMenuItem;
- N1: TMenuItem;
- Exit1: TMenuItem;
- Bevel1: TBevel;
- Picbuf1: TPicbuf;
- Jpgoptions: TRadioGroup;
- FileType: TComboBox;
- ImageCompressionLevel: TSpinEdit;
- procedure FormCreate(Sender: TObject);
- procedure SaveImageAs1Click(Sender: TObject);
- procedure FileTypeChange(Sender: TObject);
- procedure LoadImage1Click(Sender: TObject);
- procedure Exit1Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- {See Below}
- procedure SetSaveModes(SetPicbuf:TPicbuf);
- {See Below}
- function GetImageFormat(FileName:String):Integer;
- {This Returns 'True' if the supplied filename is valid}
- function ValidImageFormat(FileName:String):Boolean;
- {This Returns FileImage format as text ie '*.BMP'}
- function ImageFormatasString(FileFormat:Integer):String;
-
- implementation
-
- {$R *.DFM}
-
-
- {-------------------------------------------------------------------------------}
- {Defaults on 'Form' creation}
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- Application.HintPause:=10;
- Application.HintColor:=clAqua;
-
- {Make Sure ComboBox and OpenDialog Filters are in Sync and set to *.*}
- FileType.ItemIndex:=0;
- OpenDialog.FilterIndex:=0;
- end;
-
-
- {-------------------------------------------------------------------------------}
- {Load an Image into the Picbuf}
- procedure TForm1.LoadImage1Click(Sender: TObject);
- begin
-
- {Display Common Dialog Control}
- if OpenDialog.Execute then
- begin
-
- {Set Name}
- Picbuf1.Filename:=OpenDialog.FileName;
-
- {Load Image}
- Picbuf1.Load;
- end;
- end;
-
-
- {-------------------------------------------------------------------------------}
- {Whenever the user selects a Filetype [with the ComboBox], update the Common
- Dialog Controls Filter Index to reflect the Change - Keep them in Sync.}
- procedure TForm1.FileTypeChange(Sender: TObject);
- begin
- OpenDialog.FilterIndex := FileType.ItemIndex + 1;
- end;
-
-
- {-------------------------------------------------------------------------------}
- {Save the Visible Image.
-
- Uses three function Calls. 'GetImageformat' is used to determine the ImageFormat
- number of the file to be Saved. This is required in case the user types in the
- file path and name, rather than using the pre-picked format [combobox setting].
-
- Prior to saving the Image, the 'SetSaveModes' function is called. Pass the Picbuf
- to be saved to this function. Within that function, if the user is going to be
- saving a 'JPG' file, the various Write Options are Set - See Below.
-
- Finally prior to Displaying the OpenDialog Control, the Filename is set to the
- extension of the format the user has selected. If you do not do this, then the
- Dialog remembers the Name and Path of the last file to be loaded. This can create
- a problem, since we may wish to save the file in a new format - not required but
- catches a potential error. 'ImageFormatasString' returns the file extension based
- on the supplied format number. By doing this, it also forces the Dialog to list
- all the files of the selected file type.}
- procedure TForm1.SaveImageAs1Click(Sender: TObject);
- begin
- {Set Open Dialog to FileType ComboBox Selection}
- OpenDialog.FilterIndex := FileType.ItemIndex + 1;
-
- {We Set the 'Filename' Property to the Extension Selected by the User - this stops
- the problem of the Dialog Box remembering the last Filename and format - since
- we may wish to alter the format it is Saved As.}
- OpenDialog.FileName := ImageFormatasString(FileType.ItemIndex); {See Below}
-
- {Display Dialog}
- if OpenDialog.Execute then
- begin
-
- {Update the ComboBox to FileName's Format - Overrides selected type if the user typed
- in a new name and extension}
- FileType.ItemIndex := GetImageFormat(OpenDialog.FileName);
-
- {Call Procedure to Set Write Options - Sets JPG Options}
- SetSaveModes(Picbuf1);
-
- {Set Filename}
- Picbuf1.FileName := OpenDialog.FileName;
-
- {Store Image if it has a valid extension - See Function Below}
- if ValidImageFormat(Picbuf1.FileName) then
- Picbuf1.Store
- else
- MessageDlg('File Name or Extension is not Valid', mtConfirmation, [mbOK], 0);
- end;
- end;
-
-
- {-------------------------------------------------------------------------------}
- {This Procedure is used to set a 'Picbufs' Save Options. To use it simply call the
- function, passing the name of the Picbuf you wish to set -
-
- - SetSaveModes(MyPicbuf);
-
- Notice the use of 'With SETPICBUF do'. From that point on you need make no
- further reference to the Picbuf. You need just reference the properties you
- wish to set.}
- procedure SetSaveModes(SetPicbuf:TPicbuf);{Refer to this name within the procedure}
- begin
-
- with SetPicbuf do {Reference Picbuf - this is the Picbuf sent to the Procedure}
- begin
- ImageFormat:= Form1.FileType.ItemIndex; {SetImage Format - Based on ComboBox}
- ImageCompression := 0; {Default to NO compression as default}
-
- if Imageformat = 7 then {JPG - Set Write Option Based on Radio Buttons}
- begin
- WriteOption := Form1.Jpgoptions.ItemIndex; {Set Write Option Based on Option's}
- ImageCompression := Form1.ImageCompressionlevel.Value {Set JPG Compression level}
-
- end;
- end;
- end;
-
- {-------------------------------------------------------------------------------}
- {This Function returns the 'Integer format' number of a given file name. Pass any
- file path and name to the function and it will return the ImageFormat Number.
- Note the use of 'Result'. Any function you create by default has an in-built
- 'Result' variable. So when you call a function your result is automatically
- available for use.
-
- For example:-
-
- ImageType.ItemIndex := GetImageFormat(OpenDialog.Filename);
-
- This sets the ItemIndex to the value as returned by the function. Note how we
- pass the function a string - eg a 'FileName' returned by a Common Dialog Control}
- function GetImageFormat(FileName:String):Integer;
- Var
- Temp:String;
- begin
- Temp := UpperCase(Filename);{Convert FileName to Upper Case}
- Result:=0;{Default result if no recognised match is found - *.*}
- if Pos('.TIF', Temp ) > 0 then Result:= 1;
- if Pos('.TGA', Temp ) > 0 then Result:= 2;
- if Pos('.BMP', Temp ) > 0 then Result:= 3;
- if Pos('.GIF', Temp ) > 0 then Result:= 4;
- if Pos('.DIB', Temp ) > 0 then Result:= 5;
- if Pos('.PCX', Temp ) > 0 then Result:= 6;
- if Pos('.JPG', Temp ) > 0 then Result:= 7;
- if Pos('.MSP', Temp ) > 0 then Result:= 8;
- if Pos('.FIF', Temp ) > 0 then Result:= 9;
- if Pos('.PNG', Temp ) > 0 then Result:= 10;
- {Refer to Delphi On-line Help for 'POS' function}
- end;
-
- {-------------------------------------------------------------------------------}
- {This evaluates to true if the supplied filename is Valid}
- function ValidImageFormat(FileName:String):Boolean;
- Var
- Temp:String;
- begin
- Temp := UpperCase(Filename);{Convert FileName to Upper Case}
- Result:= False;{Default result if no recognised match is found - False}
- if Pos('.TIF', Temp ) > 0 then Result:= True;
- if Pos('.TGA', Temp ) > 0 then Result:= True;
- if Pos('.BMP', Temp ) > 0 then Result:= True;
- if Pos('.GIF', Temp ) > 0 then Result:= True;
- if Pos('.DIB', Temp ) > 0 then Result:= True;
- if Pos('.PCX', Temp ) > 0 then Result:= True;
- if Pos('.JPG', Temp ) > 0 then Result:= True;
- if Pos('.MSP', Temp ) > 0 then Result:= True;
- if Pos('.FIF', Temp ) > 0 then Result:= True;
- if Pos('.PNG', Temp ) > 0 then Result:= True;
- end;
-
- {This Returns FileImage format as Text ie '*.BMP' from the supplied Integer format}
- function ImageFormatasString(FileFormat:Integer):String;
- begin
- Result:= '*.*';{Default result - False}
- if FileFormat = 1 then Result:= '.TIF';
- if FileFormat = 2 then Result:= '.TGA';
- if FileFormat = 3 then Result:= '.BMP';
- if FileFormat = 4 then Result:= '.GIF';
- if FileFormat = 5 then Result:= '.DIB';
- if FileFormat = 6 then Result:= '.PCX';
- if FileFormat = 7 then Result:= '.JPG';
- if FileFormat = 8 then Result:= '.MSP'; {PaintBrush}
- if FileFormat = 9 then Result:= '.FIF';
- if FileFormat = 10 then Result:= '.PNG';
- end;
-
- {--------------------------------------------------------------------------------}
- {Exit}
- procedure TForm1.Exit1Click(Sender: TObject);
- begin
- Halt;
- end;
-
- end.
-