Programming C++Builder How to load and save and replace graphics files in CBuilder?

How to load and save graphics files?

Graphic images that exist only for the duration of one running of an application are of very limited value. Often, you either want to use the same picture every time, or you want to save a created picture for later use. The image component makes it easy to load pictures from a file and save them again.

The components you use to load, save, and replace graphic images support many graphic formats including bitmap files, metafiles, glyphs, and so on. They also support installable graphic classes.

The way to load and save graphics files is the similar to any other files and is described in these topics:

  1. Loading a picture from a file.
  2. Saving a picture to a file.
  3. Replacing the picture.

How to load a picture from a file?

Your application should provide the ability to load a picture from a file if your application needs to modify the picture or if you want to store the picture outside the application so a person or another application can modify the picture.

To load a graphics file into an image control, call the LoadFromFile method of the image control's Picture object.
The following code gets a file name from an open picture file dialog box, and then loads that file into an image control named Image:

void __fastcallTForm1::Open1Click(TObject *Sender)
{
  if (OpenPictureDialog1->Execute())
  {
    CurrentFile = OpenPictureDialog1->FileName;
    Image->Picture->LoadFromFile(CurrentFile);
  }
}

How to save the picture to a file?

The picture object can load and save graphics in several formats, and you can create and register your own graphic-file formats so that picture objects can load and store them as well.

To save the contents of an image control in a file, call the SaveToFile method of the image control's Picture object. The SaveToFile method requires the name of a file in which to save. If the picture is newly created, it might not have a file name, or a user might want to save an existing picture in a different file. In either case, the application needs to get a file name from the user before saving, as shown in the next section.

The following pair of event handlers, attached to the File|Save and File|Save As menu items, respectively, handle the resaving of named files, saving of unnamed files, and saving existing files under new names.

void __fastcallTForm1::Save1Click(TObject *Sender)
{
  if (!CurrentFile.IsEmpty())
    Image->Picture->SaveToFile(CurrentFile);   // save if already named
else SaveAs1Click(Sender);                     // otherwise get a name
}

void __fastcallTForm1::SaveAs1Click(TObject *Sender)
{
  if (SaveDialog1->Execute())              // get a file name
  {
    CurrentFile = SaveDialog1->FileName;  // save user-specified name
    Save1Click(Sender);                   // then save normally
  }
}

How to replace the picture in an image control?

You can replace the picture in an image control at any time. If you assign a new graphic to a picture that already has a graphic, the new graphic replaces the existing one.

To replace the picture in an image control, assign a new graphic to the image control's Picture object. Creating the new graphic is the same process you used to create the initial graphic , but you should also provide a way for the user to choose a size other than the default size used for the initial graphic. An easy way to provide that option is to present a dialog box.

With such a dialog box in your project, add an include statement for BMPDlg.hpp in the .cpp file for your main form. You can then attach an event handler to the File|New menu item's OnClick event. Here's an example:

void __fastcall TForm1::New1Click(TObject *Sender)
{
     Graphics::TBitmap *Bitmap;
  // make sure focus is on width field
   NewBMPForm->ActiveControl = NewBMPForm->WidthEdit;
  // initialize to current dimensions as default ...
     NewBMPForm->WidthEdit->Text = IntToStr(Image->Picture->Graphic->Width);
     NewBMPForm->HeightEdit->Text = IntToStr(Image->Picture->Graphic->Height);
   if (NewBMPForm->ShowModal() != IDCANCEL){        // if user does not cancel dialog...
        Bitmap = new Graphics::TBitmap();             // create a new bitmap object

     // use specified dimensions
      Bitmap->Width = StrToInt(NewBMPForm->WidthEdit->Text);
        Bitmap->Height = StrToInt(NewBMPForm->HeightEdit->Text);
        Image->Picture->Graphic = Bitmap;            // replace graphic with new bitmap
        CurrentFile = EmptyStr;                      //indicate unnamed file
        delete Bitmap;
     }
}

Note: Assigning a new bitmap to the picture object's Graphic property causes the picture object to copy the new graphic, but it does not take ownership of it. The picture object maintains its own internal graphic object. Because of this, the previous code frees the bitmap object after making the assignment.


Add comment


Security code
Refresh

Programming - C++Builder