Sign in Join
Wiki copytemporary

BGRABitmap tutorial 2/es: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: BGRABitmap tutorial 2/es

OldNewLine
1+[[BGRABitmap tutorial/es|**Home**]] | [[BGRABitmap tutorial 1/es|**Tutorial 1**]] | **Tutorial 2** | [[BGRABitmap tutorial 3/es|**Tutorial 3**]] | [[BGRABitmap tutorial 4/es|**Tutorial 4**]] | [[BGRABitmap tutorial 5/es|**Tutorial 5**]] | [[BGRABitmap tutorial 6/es|**Tutorial 6**]] | [[BGRABitmap tutorial 7/es|**Tutorial 7**]] | [[BGRABitmap tutorial 8/es|**Tutorial 8**]] | [ **Tutorial 9**](</index.php?title=BGRABitmap_tutorial_9/es&action=edit&redlink=1> "BGRABitmap tutorial 9/es \(page does not exist\)") | [[BGRABitmap tutorial 10/es|**Tutorial 10**]] | [[BGRABitmap tutorial 11/es|**Tutorial 11**]] | [[BGRABitmap tutorial 12/es|**Tutorial 12**]] | [Edit](<http://wiki.lazarus.freepascal.org/index.php?title=Template:BGRABitmap_tutorial_index/es&action=edit>)
2+
3+Esta tutoría muestra como cargar una imágen y dibujarla en una forma.
4+
5+## Contents
6+
7+ * 1 Crear un nuevo proyecto
8+ * 2 Carga el bitmap
9+ * 3 Dibuja el bitmap
10+ * 4 Código
11+ * 5 Ejecuta el programa
12+ * 6 Centrar la imágen
13+ * 7 Estirar la imágen
14+
15+### Crear un nuevo proyecto
16+
17+Crea un nuevo proyecto y agrega referencias a [[BGRABitmap]] de la misma forma que se explica en [[BGRABitmap tutorial/es|la primer tutoría.]]
18+
19+### Carga el bitmap
20+
21+Copia una imagen al directorio de tu proyecto. Supongamos que su nombre es _image.png_.
22+
23+Agrega una variable privada a la forma principal para guardar la imágen:
24+```
25+TForm1 = class(TForm)
26+ private
27+ { private declarations }
28+ image: TBGRABitmap;
29+ public
30+ { public declarations }
31+ end;
32+```
33+
34+Carga la imágen cuando la forma es creada. Para hacer esto haz doble clic en la forma, un procedimiento debería aparecer en el editor de código. Agrega la instrucción de cargado :
35+```
36+procedure TForm1.FormCreate(Sender: TObject);
37+ begin
38+ image := TBGRABitmap.Create('image.png');
39+ end;
40+```
41+
42+### Dibuja el bitmap
43+
44+Añade un manejador OnPaint. Para hacer esto, selecciona la forma principal, luego ve al inspector de objetos, en la pestaña de eventos y haz doble clic en la linea OnPaint. Luego, agrega el código de dibujo :
45+```
46+procedure TForm1.FormPaint(Sender: TObject);
47+ begin
48+ image.Draw(Canvas,0,0,True);
49+ end;
50+```
51+
52+Fijate que el último parámetro está establecido en 'True' (verdadero) que sinifica opaco. Si quieres tomar los píxeles transparentes en cuenta, codificados en el canal alpha, debes usar 'False'. Pero puede ser lento usar dibujo transparente en un 'canvas' (papel) estándar, entonces si no es necesario usa dibujo opaco solamente.
53+
54+### Código
55+
56+Finalmente deberías obtener algo como esto :
57+```
58+unit UMain;
59+
60+ {$mode objfpc}{$H+}
61+
62+ interface
63+
64+ uses
65+ Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
66+ BGRABitmap, BGRABitmapTypes;
67+
68+ type
69+ { TForm1 }
70+
71+ TForm1 = class(TForm)
72+ procedure FormCreate(Sender: TObject);
73+ procedure FormPaint(Sender: TObject);
74+ private
75+ { private declarations }
76+ image: TBGRABitmap;
77+ public
78+ { public declarations }
79+ end;
80+
81+ var
82+ Form1: TForm1;
83+
84+ implementation
85+
86+ { TForm1 }
87+
88+ procedure TForm1.FormCreate(Sender: TObject);
89+ begin
90+ image := TBGRABitmap.Create('image.png');
91+ end;
92+
93+ procedure TForm1.FormPaint(Sender: TObject);
94+ begin
95+ image.Draw(Canvas,0,0,True);
96+ end;
97+
98+ initialization
99+ {$I UMain.lrs}
100+
101+ end.
102+```
103+
104+### Ejecuta el programa
105+
106+Deberías ver una forma con una imágen dibujada en ella en la esquina superior-izquierda.
107+
108+[![BGRATutorial2.png](/wiki-images/d/db/BGRATutorial2.png)](</File:BGRATutorial2.png>)
109+
110+### Centrar la imágen
111+
112+Quizás quieras centrar la imágen en la forma. Para hacer esto, modifica el procedimiento FormPaint :
113+```
114+procedure TForm1.FormPaint(Sender: TObject);
115+ var ImagePos: TPoint;
116+ begin
117+ ImagePos := Point( (ClientWidth - Image.Width) div 2,
118+ (ClientHeight - Image.Height) div 2 );
119+
120+ // test de posición negativa
121+ if ImagePos.X < 0 then ImagePos.X := 0;
122+ if ImagePos.Y < 0 then ImagePos.Y := 0;
123+
124+ image.Draw(Canvas,ImagePos.X,ImagePos.Y,True);
125+ end;
126+```
127+
128+Para computar la posición, necesitamos calcular el espacio entre la imágen y el borde izquierdo (coordenada X) y el espacio entre la imágen y el borde superior (coordenada Y). La expresion ClientWidth - Image.Width regresa el espacio horizontal disponible, y si lo dividimos por dos obtenemos el margen izquierdo.
129+
130+El resultado puede ser negativo si la imágen es mas grande que el ancho del cliente. En este caso, el margen es establecido como cero.
131+
132+Puedes ejecutar el programa y ver si funciona. Fijate que sucede si removes el test de posición negativa.
133+
134+### Estirar la imágen
135+
136+Para estirar la imágen, necesitamos crear una imágen temporalmente estirada :
137+```
138+procedure TForm1.FormPaint(Sender: TObject);
139+ var stretched: TBGRABitmap;
140+ begin
141+ stretched := image.Resample(ClientWidth, ClientHeight) as TBGRABitmap;
142+ stretched.Draw(Canvas,0,0,True);
143+ stretched.Free;
144+ end;
145+```
146+
147+Por defecto, esto usa el estiramiento fino, pero puedes precisar si quieres usar el estiramiento simple (mas rápido) :
148+```
149+stretched := image.Resample(ClientWidth, ClientHeight, rmSimpleStretch) as TBGRABitmap;
150+```
151+
152+[[BGRABitmap tutorial 1/es|Primer tutoría]] | [[BGRABitmap tutorial 3/es|Siguiente tutoría (dibujando con el ratón)]]
153+
154+---
155+*From the Free Pascal wiki, page "BGRABitmap tutorial 2/es", as the Internet Archive kept it on 2025-05-16. 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).*

Back to history