BGRABitmap tutorial 2/de: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: BGRABitmap tutorial 2/de
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Dieses Tutorial zeigt, wie Sie ein Bild laden und und es auf ein Formular zeichnen können. | |
| 2 | + | ||
| 3 | + | ### Erzeugen Sie ein neues Projekt | |
| 4 | + | ||
| 5 | + | Erzeugen Sie ein neues Projekt und fügen Sie eine Referenz auf [[BGRABitmap/de|BGRABitmap]] hinzu, genau so wie im [[BGRABitmap tutorial/de|ersten Tutorial]]. | |
| 6 | + | ||
| 7 | + | ### Laden Sie die Bitmap | |
| 8 | + | ||
| 9 | + | Kopieren Sie ein Bild in Ihr Projektverzeichnis. Nehmen wir an, sein Name sei *image.png*. | |
| 10 | + | ||
| 11 | + | Fügen Sie dem Hauptformular eine private Variable zum Speichern des Bildes hinzu: | |
| 12 | + | ||
| 13 | + | ``` pascal | |
| 14 | + | TForm1 = class(TForm) | |
| 15 | + | private | |
| 16 | + | { private Deklarationen } | |
| 17 | + | image: TBGRABitmap; | |
| 18 | + | public | |
| 19 | + | { öffentliche Deklarationen } | |
| 20 | + | end; | |
| 21 | + | ``` | |
| 22 | + | ||
| 23 | + | Laden Sie das Bild, wenn das Formular erzeugt wird. Dazu doppelklicken Sie auf das Formular und eine Prozedur erscheint im Codeeditor. Fügen Sie die Anweisung zum Laden hinzu: | |
| 24 | + | ||
| 25 | + | ``` pascal | |
| 26 | + | procedure TForm1.FormCreate(Sender: TObject); | |
| 27 | + | begin | |
| 28 | + | image := TBGRABitmap.Create('image.png'); | |
| 29 | + | end; | |
| 30 | + | ``` | |
| 31 | + | ||
| 32 | + | ### Zeichnen Sie die Bitmap | |
| 33 | + | ||
| 34 | + | Fügen Sie einen OnPaint-Handler hinzu. Dazu wählen Sie das Hauptformular aus, gehen zum Objektinspektor, zum Karteireiter Ereignisse und doppelklicken Sie auf die Zeile 'OnPaint'. Dann fügen Sie den Zeichenbefehl ein: | |
| 35 | + | ||
| 36 | + | ``` pascal | |
| 37 | + | procedure TForm1.FormPaint(Sender: TObject); | |
| 38 | + | begin | |
| 39 | + | image.Draw(Canvas,0,0,True); | |
| 40 | + | end; | |
| 41 | + | ``` | |
| 42 | + | ||
| 43 | + | Beachten Sie, dass der letzte Parameter auf 'True' gesetzt ist (was undurchsichtig = opak bedeutet). Wenn Sie wollen, dass die im Alphakanal kodierten transparenten Pixel berücksichtigt werden, dann verwenden Sie stattdessen 'False'. Aber das transparente Zeichnen auf der Standardzeichenfläche kann langsam sein. Well es also nicht notwendig ist, zeichnen Sie nur opak. | |
| 44 | + | ||
| 45 | + | ### Resultierender Code | |
| 46 | + | ||
| 47 | + | Sie sollten jetzt den folgenden Code haben: | |
| 48 | + | ||
| 49 | + | ``` pascal | |
| 50 | + | unit UMain; | |
| 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 Deklarationen } | |
| 68 | + | image: TBGRABitmap; | |
| 69 | + | public | |
| 70 | + | { öffentliche Deklarationen } | |
| 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. | |
| 94 | + | ``` | |
| 95 | + | ||
| 96 | + | ### Starten Sie das Programm | |
| 97 | + | ||
| 98 | + | Sie sollten ein Formular mit dem Bild in der oberen, linken Ecke sehen. | |
| 99 | + | ||
| 100 | + | <figure> | |
| 101 | + | <img src="BGRATutorial2.png" title="BGRATutorial2.png" /> | |
| 102 | + | <figcaption>BGRATutorial2.png</figcaption> | |
| 103 | + | </figure> | |
| 104 | + | ||
| 105 | + | ### Zentrieren des Bildes | |
| 106 | + | ||
| 107 | + | Vielleicht wollen Sie das Bild auf dem Formular zentrieren. Dazu verändern Sie die Prozedur 'FormPaint': | |
| 108 | + | ||
| 109 | + | ``` pascal | |
| 110 | + | procedure TForm1.FormPaint(Sender: TObject); | |
| 111 | + | var ImagePos: TPoint; | |
| 112 | + | begin | |
| 113 | + | ImagePos := Point( (ClientWidth - Image.Width) div 2, | |
| 114 | + | (ClientHeight - Image.Height) div 2 ); | |
| 115 | + | ||
| 116 | + | // Test auf eine negative Position | |
| 117 | + | if ImagePos.X < 0 then ImagePos.X := 0; | |
| 118 | + | if ImagePos.Y < 0 then ImagePos.Y := 0; | |
| 119 | + | ||
| 120 | + | image.Draw(Canvas,ImagePos.X,ImagePos.Y,True); | |
| 121 | + | end; | |
| 122 | + | ``` | |
| 123 | + | ||
| 124 | + | Um die Position zu bestimmen, berechnen wir den Platz zwischen dem Bild und dem linken Fensterrand (X-Koordinate) und den Platz zwischen dem Bild und dem oberen Fensterrand (Y-Koordinate). Der Ausdruck 'ClientWidth - Image.Width' gibt den verfügbaren horizontalen Platz zurück, und wir dividieren durch 2 und erhalten den linken Randabstand. | |
| 125 | + | ||
| 126 | + | Das Ergebnis kann negativ sein, wenn das Bild breiter ist als ClientWidth. In diesem Fall setzen wir den Randabstand auf Null. | |
| 127 | + | ||
| 128 | + | Starten Sie das Programm und beobachten Sie ob es funktioniert. Achten Sie darauf was passiert, wenn Sie den Test auf eine negative Position entfernen. | |
| 129 | + | ||
| 130 | + | ### Strecken des Bildes | |
| 131 | + | ||
| 132 | + | Um das Bild zu strecken, müssen wir ein temporäres gestrecktes Bild erzeugen: | |
| 133 | + | ||
| 134 | + | ``` pascal | |
| 135 | + | procedure TForm1.FormPaint(Sender: TObject); | |
| 136 | + | var stretched: TBGRABitmap; | |
| 137 | + | begin | |
| 138 | + | stretched := image.Resample(ClientWidth, ClientHeight) as TBGRABitmap; | |
| 139 | + | stretched.Draw(Canvas,0,0,True); | |
| 140 | + | stretched.Free; | |
| 141 | + | end; | |
| 142 | + | ``` | |
| 143 | + | ||
| 144 | + | Als Vorgabe wird das Bild mit feiner Auflösung neu gezeichnet, aber Sie können angeben, ob Sie stattdessen eine einfache Streckung, die schneller ist, verwenden wollen: | |
| 145 | + | ||
| 146 | + | ``` pascal | |
| 147 | + | stretched := image.Resample(ClientWidth, ClientHeight, rmSimpleStretch) as TBGRABitmap; | |
| 148 | + | ``` | |
| 149 | + | ||
| 150 | + | Ebenso können Sie auch den Interpolationsfilter angeben mit der Eigenschaft 'ResampleFilter': | |
| 151 | + | ||
| 152 | + | ``` pascal | |
| 153 | + | image.ResampleFilter := rfMitchell; | |
| 154 | + | stretched := image.Resample(ClientWidth, ClientHeight) as TBGRABitmap; | |
| 155 | + | ``` | |
| 156 | + | ||
| 157 | + | [[BGRABitmap tutorial 1/de|Erstes Tutorial]] \| [[BGRABitmap tutorial 3/de|Nächstes Tutorial (Zeichnen mit der Maus)]] | |
| 158 | + | ||
| 159 | + | --- | |
| 160 | + | *From the Free Pascal wiki, page "BGRABitmap tutorial 2/de", 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).* |
