UsingPictureDialogs (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example uses an image, two buttons, an open picture dialog, and a save picture dialog on a form. Clicking 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.

Code

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  Image1->Canvas->Refresh();
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  // Execute an open picture dialog.
  if (OpenPictureDialog1->Execute())
    // First check if the 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 the 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);
}

Uses