Remplacement de l'image

De RAD Studio
Aller à : navigation, rechercher

Remonter à Chargement et enregistrement de fichiers graphiques


Il est possible à tout moment de remplacer l'image d'un contrôle image. Si un nouveau graphique est affecté à un objet Picture ayant déjà un graphique, le nouveau graphique remplace l'ancien.

Pour remplacer l'image contenue dans un contrôle image, affectez un nouveau graphique à l'objet Picture du contrôle image.

La création d'un nouveau graphique passe par le même processus que la création du premier graphique, mais il faut également permettre à l'utilisateur de choisir une taille différente de celle utilisée par défaut pour le graphique initial. Un moyen simple de proposer une telle option est de présenter une boîte de dialogue.

Une boîte de dialogue étant dans votre projet, ajoutez-la à la clause uses de l'unité de votre fiche principale. Vous pouvez ensuite attacher un gestionnaire à l'événement OnClick de l'élément de menu Fichier > Nouveau. Par exemple :

procedure TForm1.New1Click(Sender: TObject);
var
  Bitmap: TBitmap; { temporary variable for the new bitmap }
begin
  with NewBMPForm do
  begin
    ActiveControl := WidthEdit; { make sure focus is on width field }
    WidthEdit.Text := IntToStr(Image.Picture.Graphic.Width);
    { use current dimensions... }
    HeightEdit.Text := IntToStr(Image.Picture.Graphic.Height); { ...as default }
    if ShowModal <
      >
    idCancel then { continue if user doesn"t cancel dialog box }

    begin
      Bitmap := TBitmap.Create; { create fresh bitmap object }
      Bitmap.Width := StrToInt(WidthEdit.Text); { use specified width }
      Bitmap.Height := StrToInt(HeightEdit.Text); { use specified height }
      Image.Picture.Graphic := Bitmap; { replace graphic with new bitmap }
      CurrentFile := " "; { indicate unnamed file }
      Bitmap.Free;
    end;
  end;
end;
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;
	}
}

Remarque :  L'affectation d'un nouveau bitmap à la propriété Graphic de l'objet Picture oblige celui-ci à copier le nouveau graphique, mais il ne devient pas son propriétaire. L'objet Picture maintient son propre object graphique interne. C'est pour cela que le code précédent libère l'objet bitmap une fois que l'affectation est effectuée.

Voir aussi