RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
Graphics.GraphicExtension Function

Returns the default file-name extension of a graphics object.

Pascal
function GraphicExtension(GraphicClass: TGraphicClass): string;
C++
AnsiString GraphicExtension(TGraphicClass GraphicClass);

Graphics

Call GraphicExtension to obtain a default extension value for the DefaultExt property of an Open, Open Picture, Save Picture or Save dialog. GraphicExtension returns the default extension for the type of graphics object specified by the GraphicClass parameter. In Delphi, the TGraphicClass type is the class type for descendants TGraphic

These are the file extensions returned for each built-in graphics class:

Graphic class 
File extension returned 
bmp  
TIcon  
ico  
emf  

If custom graphics classes register a file extension, GraphicExtension will return that extension when passed the custom graphics class as an argument.  

C++ Examples: 

 

/*
The following example allows users to type the name of a
graphics class into an edit control and when a button is
pressed, the name of the default extension for that class is
displayed in another edit control.  In the form’s OnCreate
event handler (or a similar place) you must register the
graphics classes so that GetClass can find them.
*/
TMetaClass *MetaClass;

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TClassFinder *myClassFinder = new TClassFinder(MetaClass, False);
  TGraphicClass mygraphclass = (TGraphicClass) (myClassFinder->GetClass(Edit2->Text));
//  TGraphicClass mygraphclass = (TGraphicClass) GetClass(Edit2->Text);
  Edit1->Text = GraphicExtension(mygraphclass);
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  MetaClass = __classid(TIcon); // ico
  RegisterClasses(&MetaClass, 0);
  MetaClass = __classid(Graphics::TBitmap); // bmp
  RegisterClasses(&MetaClass, 0);
  MetaClass = __classid(TMetafile); // emf
  RegisterClasses(&MetaClass, 0);
  MetaClass = __classid(TShape);  // nographic extension
  RegisterClasses(&MetaClass, 0);
  MetaClass = __classid(TImage); // nographic extension
  RegisterClasses(&MetaClass, 0);
}
/*
This code displays a Save Picture dialog box with the
TBitmap default extension that is added automatically to
files that are given no extension. Confirm that the
bitmap file has been saved under the filename and path
specified.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  AnsiString NewFileName, OldFileName;
  TFileStream *NewFile, *OldFile;
  SavePictureDialog1->DefaultExt = GraphicExtension(__classid(Graphics::TBitmap));
  SavePictureDialog1->Filter = GraphicFilter(__classid(Graphics::TBitmap));
  OldFileName = "factory.bmp";
  if (SavePictureDialog1->Execute())
  {
    NewFileName = SavePictureDialog1->FileName;
    OldFile = new TFileStream(OldFileName, (fmOpenRead | fmShareDenyWrite));
    try
    {
      NewFile = new TFileStream(NewFileName, (fmCreate | fmShareDenyRead));  
      try
      {
        NewFile->CopyFrom(OldFile, OldFile->Size);
      }
      __finally
      {
        FreeAndNil(&NewFile);
      };
    }
    __finally
    {
      FreeAndNil(&OldFile);
    };
  };
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  Image1->Picture->LoadFromFile("../factory.bmp");
}

 

Delphi Examples: 

{
The following example allows users to type the name of a
graphics class into an edit control and when a button is
pressed, the name of the default extension for that class is
displayed in another edit control.  In the form’s OnCreate
event handler (or a similar place) you must register the
graphics classes so that GetClass can find them.
}

uses ExtCtrls;

procedure TForm1.Button1Click(Sender: TObject);
var
  myClassFinder: TClassFinder;
  mygraphclass: TGraphicClass;
begin
  { make sure the classes are registered so that GetClass will work -- }
  { Usually, this goes in the initialization section where it is only executed once }
  RegisterClasses([TIcon, TBitmap, TButton, TShape, TImage, TMetafile]);
  try
    myClassFinder := TClassFinder.Create(TIcon, False);
    mygraphclass := TGraphicClass(myClassFinder.GetClass(Edit2.Text));
//    mygraphclass := TGraphicClass(GetClass(Edit2.Text));  // works as well
    Edit1.Text := GraphicExtension(mygraphclass);
  except
    on EAccessViolation do
      MessageDlg('Invalid class name.', mtError, [mbOK], 0)
  end;
end;
{
This code displays a Save Picture dialog box with the
TBitmap default extension that is added automatically to
files that are given no extension. Confirm that the
bitmap file has been saved under the filename and path
specified.
}
procedure TForm1.Button1Click(Sender: TObject);
var NewFileName, OldFileName: string;
    NewFile, OldFile: TFileStream;
begin
  SavePictureDialog1.DefaultExt := GraphicExtension(TBitmap);
  SavePictureDialog1.Filter := GraphicFilter(TBitmap);
  OldFileName := 'factory.bmp';
  if SavePictureDialog1.Execute then
  begin
    NewFileName := SavePictureDialog1.FileName;
    OldFile := TFileStream.Create(
      OldFileName, fmOpenRead or fmShareDenyWrite);
    try
      NewFile := TFileStream.Create(
        NewFileName, fmCreate or fmShareDenyRead);
      try
        NewFile.CopyFrom(OldFile, OldFile.Size);
      finally
        FreeAndNil(NewFile);
      end;
    finally
      FreeAndNil(OldFile);
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Image1.Picture.LoadFromFile('factory.bmp');
end;

 

GraphicFileMask 

GraphicFilter 

TPicture 

TOpenDialog 

TOpenPictureDialog 

TSavePictureDialog

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!