RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TOpenPictureDialog Class

TOpenPictureDialog displays a graphics-file selection dialog.

Pascal
TOpenPictureDialog = class(TOpenDialog);
C++
class TOpenPictureDialog : public TOpenDialog;

TOpenPictureDialog displays a modal Windows dialog box for selecting and opening graphics files. This component is just like TOpenDialog, except that it includes a rectangular preview region. If the selected image can be read by TPicture, it is displayed in the preview region; supported file types include bitmap (.BMP), icon (.ICO), Windows metafile (.WMF), and enhanced Windows metafile (.EMF). If the selected image cannot be displayed, "(None)" appears in the preview region. 

If the user selects a file of an unrecognized type, TPicture raises an EInvalidGraphic exception.  

C++ Examples: 

 

/*
This example uses an image, two buttons, an open picture dialog
and a save picture dialog on a form. Clicking on the Open/Save button
executes the open/save picture dialog.
An exception is raised when trying to open a file that
does not exist, or when trying to overwrite a file using the
save dialog.
*/
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  Image1->Canvas->Refresh();
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  // execute an open picture dialog
  if (OpenPictureDialog1->Execute())
    // first check if file exists
    if (FileExists(OpenPictureDialog1->FileName))
      // if it exists, load the data into the image component
      Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName);
    else
      // otherwise raise an exception
      throw(Exception("File does not exist."));
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  // execute a save picture dialog
  if (SavePictureDialog1->Execute())
    // first check if file exists
    if (FileExists(SavePictureDialog1->FileName))
      // if it exists, raise an exception
      throw(Exception("File already exists. Cannot overwrite."));
    else
      // otherwise, save the image data into the file
      Image1->Picture->SaveToFile(SavePictureDialog1->FileName);
}

 

Delphi Examples: 

{
This example uses an image, two buttons, an open picture dialog
and a save picture dialog on a form. Clicking on the Open/Save button
executes the open/save picture dialog.
An exception is raised when trying to open a file that
does not exist, or when trying to overwrite a file using the
save dialog.
}
procedure TForm1.Button1Click(Sender: TObject);
begin
  { execute an open picture dialog }
  if OpenPictureDialog1.Execute then
    { first check if file exists }
    if FileExists(OpenPictureDialog1.FileName) then
      { if it exists, load the data into the image component }
      Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName)
    else
      { otherwise raise an exception }
      raise Exception.Create('File does not exist.');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  { execute a save picture dialog }
  if SavePictureDialog1.Execute then
    { first check if file exists }
    if FileExists(SavePictureDialog1.FileName) then
      { if it exists, raise an exception }
      raise Exception.Create('File already exists. Cannot overwrite.')
    else
      { otherwise, save the image data into the file }
      Image1.Picture.SaveToFile(SavePictureDialog1.FileName);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Image1.Canvas.Refresh;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!