BGRABitmap tutorial 2/fr: what changed
Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 2 | - | ||
| 3 | - | ||
| 4 | - | ||
| 5 | - | ||
| 6 | - | ||
| 7 | - | ||
| 8 | - | ||
| 9 | - | ||
| 10 | - | ||
| 11 | - | ||
| 12 | - | ||
| 13 | - | ||
| 1 | + | Ce tutoriel montre comment charger une image et la dessiner sur la fenêtre. | |
| 14 | 2 | ||
| 15 | 3 | ### Création d'un nouveau projet | |
| 16 | 4 | ||
| 17 | - | ||
| 5 | + | Créez un nouveau projet et ajoutez la référence à [[BGRABitmap]], de la même façon que dans [[BGRABitmap tutorial/fr|le premier tutoriel]]. | |
| 18 | 6 | ||
| 19 | 7 | ### Chargement de l'image | |
| 20 | 8 | ||
| 21 | - | ||
| 9 | + | Copiez une image dans le répertoire du projet. Supposons que son nom soit *image.png*. | |
| 22 | 10 | ||
| 23 | - | ||
| 24 | - | ||
| 25 | - | ||
| 26 | - | ||
| 27 | - | ||
| 28 | - | ||
| 29 | - | ||
| 30 | - | ||
| 31 | - | ||
| 32 | - | ||
| 11 | + | Ajoutez une variable privée à la fenêtre principale pour stocker l'image : | |
| 33 | 12 | ||
| 34 | - | ||
| 13 | + | ``` pascal | |
| 14 | + | TForm1 = class(TForm) | |
| 15 | + | private | |
| 16 | + | { private declarations } | |
| 17 | + | image: TBGRABitmap; | |
| 18 | + | public | |
| 19 | + | { public declarations } | |
| 20 | + | end; | |
| 35 | 21 | ``` | |
| 22 | + | ||
| 23 | + | Programmez le chargement de l'image quand la fenêtre est créée. Pour cela, double-cliquez sur la fenêtre. Une procédure devrait apparaître dans l'éditeur de code. Ajoutez-y l'instruction de chargement : | |
| 24 | + | ||
| 25 | + | ``` pascal | |
| 36 | 26 | procedure TForm1.FormCreate(Sender: TObject); | |
| 37 | - | ||
| 38 | - | ||
| 39 | - | ||
| 27 | + | begin | |
| 28 | + | image := TBGRABitmap.Create('image.png'); | |
| 29 | + | end; | |
| 40 | 30 | ``` | |
| 41 | 31 | ||
| 42 | 32 | ### Dessiner l'image | |
| 43 | 33 | ||
| 44 | - | ||
| 45 | - | ||
| 34 | + | Ajoutez un gestionnaire *OnPaint*. Pour cela, cliquez sur la fenêtre, allez dans l'inspecteur d'objet, dans l'onglet événement et double-cliquez sur la ligne *OnPaint*. Ensuite, ajoutez le code suivant : | |
| 35 | + | ||
| 36 | + | ``` pascal | |
| 46 | 37 | procedure TForm1.FormPaint(Sender: TObject); | |
| 47 | - | ||
| 48 | - | ||
| 49 | - | ||
| 38 | + | begin | |
| 39 | + | image.Draw(Canvas,0,0,True); | |
| 40 | + | end; | |
| 50 | 41 | ``` | |
| 51 | 42 | ||
| 52 | - | ||
| 43 | + | Remarquez que le dernier paramètre est à *True*, ce qui signifie opaque. Si vous voulez prendre en compte les pixels transparents, encodés dans le canal alpha, vous devez mettre *False* à la place. Mais cela peut être lent de dessiner avec transparence sur un *Canvas* standard, alors si la transparence n'est pas nécessaire, utilisez le dessin opaque seulement. | |
| 53 | 44 | ||
| 54 | 45 | ### Code | |
| 55 | 46 | ||
| 56 | - | ||
| 57 | - | ||
| 47 | + | Finalement, vous devriez avoir quelque chose comme cela : | |
| 48 | + | ||
| 49 | + | ``` pascal | |
| 58 | 50 | unit UMain; | |
| 59 | - | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 63 | - | ||
| 64 | - | ||
| 65 | - | ||
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 69 | - | ||
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 73 | - | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 77 | - | ||
| 78 | - | ||
| 79 | - | ||
| 80 | - | ||
| 81 | - | ||
| 82 | - | ||
| 83 | - | ||
| 84 | - | ||
| 85 | - | ||
| 86 | - | ||
| 87 | - | ||
| 88 | - | ||
| 89 | - | ||
| 90 | - | ||
| 91 | - | ||
| 92 | - | ||
| 93 | - | ||
| 94 | - | ||
| 95 | - | ||
| 96 | - | ||
| 97 | - | ||
| 98 | - | ||
| 99 | - | ||
| 100 | - | ||
| 101 | - | ||
| 51 | + | ||
| 52 | + | {$mode objfpc}{$H+} | |
| 53 | + | ||
| 54 | + | interface | |
| 55 | + | ||
| 56 | + | uses | |
| 57 | + | Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, | |
| 58 | + | BGRABitmap, BGRABitmapTypes; | |
| 59 | + | ||
| 60 | + | type | |
| 61 | + | { TForm1 } | |
| 62 | + | ||
| 63 | + | TForm1 = class(TForm) | |
| 64 | + | procedure FormCreate(Sender: TObject); | |
| 65 | + | procedure FormPaint(Sender: TObject); | |
| 66 | + | private | |
| 67 | + | { private declarations } | |
| 68 | + | image: TBGRABitmap; | |
| 69 | + | public | |
| 70 | + | { public declarations } | |
| 71 | + | end; | |
| 72 | + | ||
| 73 | + | var | |
| 74 | + | Form1: TForm1; | |
| 75 | + | ||
| 76 | + | implementation | |
| 77 | + | ||
| 78 | + | { TForm1 } | |
| 79 | + | ||
| 80 | + | procedure TForm1.FormCreate(Sender: TObject); | |
| 81 | + | begin | |
| 82 | + | image := TBGRABitmap.Create('image.png'); | |
| 83 | + | end; | |
| 84 | + | ||
| 85 | + | procedure TForm1.FormPaint(Sender: TObject); | |
| 86 | + | begin | |
| 87 | + | image.Draw(Canvas,0,0,True); | |
| 88 | + | end; | |
| 89 | + | ||
| 90 | + | initialization | |
| 91 | + | {$I UMain.lrs} | |
| 92 | + | ||
| 93 | + | end. | |
| 102 | 94 | ``` | |
| 103 | 95 | ||
| 104 | 96 | ### Exécution du programme | |
| 105 | 97 | ||
| 106 | - | ||
| 98 | + | Vous devriez voir une fenêtre avec une image dessinée dedans au coin supérieur gauche. | |
| 107 | 99 | ||
| 108 | - | ||
| 100 | + |  | |
| 109 | 101 | ||
| 110 | 102 | ### Centrer l'image | |
| 111 | 103 | ||
| 112 | - | ||
| 113 | - | ||
| 104 | + | Vous pouvez centrer l'image sur la fenêtre. Pour cela, modifiez la procédure *FormPaint* : | |
| 105 | + | ||
| 106 | + | ``` pascal | |
| 114 | 107 | procedure TForm1.FormPaint(Sender: TObject); | |
| 115 | - | ||
| 116 | - | ||
| 117 | - | ||
| 118 | - | ||
| 119 | - | ||
| 120 | - | ||
| 121 | - | ||
| 122 | - | ||
| 123 | - | ||
| 124 | - | ||
| 125 | - | ||
| 108 | + | var ImagePos: TPoint; | |
| 109 | + | begin | |
| 110 | + | ImagePos := Point( (ClientWidth - Image.Width) div 2, | |
| 111 | + | (ClientHeight - Image.Height) div 2 ); | |
| 112 | + | ||
| 113 | + | // teste si la position est négative | |
| 114 | + | if ImagePos.X < 0 then ImagePos.X := 0; | |
| 115 | + | if ImagePos.Y < 0 then ImagePos.Y := 0; | |
| 116 | + | ||
| 117 | + | image.Draw(Canvas,ImagePos.X,ImagePos.Y,True); | |
| 118 | + | end; | |
| 126 | 119 | ``` | |
| 127 | 120 | ||
| 128 | - | ||
| 121 | + | Pour calculer la position, vous avez besoin de déterminer l'espace entre l'image et le bord gauche (coordonnée X) et l'espace entre l'image et le bord haut (coordonnée Y). L'expression *ClientWidth - Image.Width* renvoie l'espace horizontal disponible, et on divise par 2 pour obtenir la marge gauche. | |
| 129 | 122 | ||
| 130 | - | ||
| 123 | + | Le résultat peut être négatif si l'image est plus grande que la largeur cliente. Dans ce cas, la marge est mise à zéro. | |
| 131 | 124 | ||
| 132 | - | ||
| 125 | + | Vous pouvez lancer le programme et voir si cela marche. Notez ce qu'il se passe si vous enlevez le test pour la position négative. | |
| 133 | 126 | ||
| 134 | 127 | ### Étirer une image | |
| 135 | 128 | ||
| 136 | - | ||
| 137 | - | ||
| 129 | + | Pour étirer une image, vous avez besoin de créer une image temporaire étirée : | |
| 130 | + | ||
| 131 | + | ``` pascal | |
| 138 | 132 | procedure TForm1.FormPaint(Sender: TObject); | |
| 139 | - | ||
| 140 | - | ||
| 141 | - | ||
| 142 | - | ||
| 143 | - | ||
| 144 | - | ||
| 133 | + | var stretched: TBGRABitmap; | |
| 134 | + | begin | |
| 135 | + | stretched := image.Resample(ClientWidth, ClientHeight) as TBGRABitmap; | |
| 136 | + | stretched.Draw(Canvas,0,0,True); | |
| 137 | + | stretched.Free; | |
| 138 | + | end; | |
| 145 | 139 | ``` | |
| 146 | 140 | ||
| 147 | - | ||
| 148 | - | ||
| 141 | + | Par défaut, *Resample* utilise un joli ré-échantillonnage, mais vous pouvez préciser si vous voulez un ré-échantillonnage simple à la place (plus rapide) : | |
| 142 | + | ||
| 143 | + | ``` pascal | |
| 149 | 144 | stretched := image.Resample(ClientWidth, ClientHeight, rmSimpleStretch) as TBGRABitmap; | |
| 150 | 145 | ``` | |
| 151 | 146 | ||
| 152 | - | ||
| 153 | - | ||
| 147 | + | Vous pouvez choisir le filtre de ré-échantillonage en utilisant la propriété *ResampleFilter* : | |
| 148 | + | ||
| 149 | + | ``` pascal | |
| 154 | 150 | image.ResampleFilter := rfMitchell; | |
| 155 | - | ||
| 151 | + | stretched := image.Resample(ClientWidth, ClientHeight) as TBGRABitmap; | |
| 156 | 152 | ``` | |
| 157 | 153 | ||
| 158 | - | ||
| 154 | + | [[BGRABitmap tutorial 1/fr|Premier tutoriel]] \| [[BGRABitmap tutorial 3/fr|Tutoriel suivant (dessiner avec la souris)]] | |
| 159 | 155 | ||
| 160 | 156 | --- | |
| 161 | - | ||
| 157 | + | *From the Free Pascal wiki, page "BGRABitmap tutorial 2/fr", as it stood on 2020-02-10 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
