Bonjour à tous
On va faire un exemple généralisé d'affichage des images (BMP, JPEG et PNG) avec Graphics32.
Ces images sont classifiées et placées dans des dossiers différents. Dans notre cas on a quatre dossiers nommés : Dattes, Oranges, Poires et Pommes.
On va ajouter sur une fiche les composants suivants: Image32_1(ScaleMode:smOptimal), Bitmap32List1, 5 boutons (chargement, suivante, precedente, ImageNum, Echelle), Label1, Edit1, Edit2, CheckBox (EffacerAncien State : cbChecked), ProgressBar1, Combobox1 (Items : Dattes, Oranges, Poires, Pommes).
On implémente maintenant les codes :
1-Déclaration des variables :
2-Initialisation des données :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 var Form1: TForm1; indexp,indexv,indexr:integer;
3-Chargement des images :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 procedure TForm1.FormCreate(Sender: TObject); begin Edit1.Text := 'C:/images/Dattes'; suivante.Enabled:=false ; precedente.Enabled :=false ; imageNum.Enabled :=false ; end;
4-Formulation des chemins/Dossiers :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 procedure TForm1.chargementClick(Sender: TObject); var list: TStringList; i: integer; begin indexv:=0; indexr:=0; Progressbar1.Position:=0 ; if EffacerAncien.Checked=true then Bitmap32List1.Bitmaps.Clear ; list := FindAllFiles(Edit1.Text, '*.bmp;*.jpg;*.png', FALSE); Label1.Caption := Format('%d images trouvées', [list.Count]); for i := 0 to list.Count - 1 do Begin Bitmap32List1.Bitmaps.Add.Bitmap.LoadFromFile(list.Strings[i]); Progressbar1.Position:=integer(i*100 DIV (list.count-1)) ; End; list.Free; suivante.Enabled :=true ; precedente.Enabled :=true ; imageNum.Enabled :=true; end;
5-Avancer d'une image :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 procedure TForm1.ComboBox1Change(Sender: TObject); begin Case Combobox1.ItemIndex of 0: Edit1.Text:='C:/images/Dattes'; 1: Edit1.Text:='C:/images/Oranges'; 2: Edit1.Text:='C:/images/Poires'; 3: Edit1.Text:='C:/images/Pommes'; end; suivante.Enabled :=false ; precedente.Enabled :=false ; imageNum.Enabled:=false ; end;
6-Reculer d'une image :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 procedure TForm1.suivanteClick(Sender: TObject); begin Image32_1.Bitmap.Assign(Bitmap32List1.Bitmap[indexv]); indexv := (indexv + 1) mod Bitmap32List1.Bitmaps.Count; if indexv=0 then edit2.Text:=IntToStr(Bitmap32List1.Bitmaps.Count-1) else edit2.Text:=IntToStr(indexv-1) ; indexr:=indexv-2; if indexr<0 then indexr:=0; end;
7-Atteindre l'image Num :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 procedure TForm1.precedenteClick(Sender: TObject); begin try Image32_1.Bitmap.Assign(Bitmap32List1.Bitmap[Indexr]); Indexr := (Indexr - 1) mod Bitmap32List1.Bitmaps.Count; Except Image32_1.Bitmap.Assign(Bitmap32List1.Bitmap[Bitmap32List1.Bitmaps.Count-1]); Indexr:=Bitmap32List1.Bitmaps.Count-2 end; edit2.Text:=IntToStr(indexr+1) ; indexv:=indexr+2 end;
8-Echelle:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 procedure TForm1.ImageNumClick(Sender: TObject); begin indexp:=StrToInt(Edit2.Text) ; if indexp<0 then Begin indexp:=0; Edit2.Text:='0' end; if indexp>=Bitmap32List1.Bitmaps.Count then Begin indexp:=Bitmap32List1.Bitmaps.Count-1; Edit2.Text:=inttostr(Bitmap32List1.Bitmaps.Count-1) end ; Image32_1.Bitmap.Assign(Bitmap32List1.Bitmap[indexp]) ; indexv:=indexp+1; indexr:=indexp-1; end;
Merci à tous.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 procedure TForm1.EchelleClick(Sender: TObject); begin if image32_1.ScaleMode=smOptimal then begin image32_1.ScaleMode:=smStretch; Echelle.Caption:='Minimiser' ; end else begin image32_1.ScaleMode:=smOptimal; Echelle.Caption:='Agrandir' ; end; end;![]()
Partager