Difference between revisions of "BGRABitmap tutorial 2/es"

From Lazarus wiki
Jump to navigationJump to search
(New page: {{BGRABitmap_tutorial_2}} {{BGRABitmap_tutorial_index/es}} Este tutorial muestra como cargar una imágen y dibujarla en una forma. === Crear un nuevo proyecto === Crea un nuevo proyect...)
 
Line 135: Line 135:
 
<delphi>stretched := image.Resample(ClientWidth, ClientHeight, rmSimpleStretch) as TBGRABitmap;</delphi>
 
<delphi>stretched := image.Resample(ClientWidth, ClientHeight, rmSimpleStretch) as TBGRABitmap;</delphi>
  
[[BGRABitmap tutorial/es|Primer tutorial]] | [[BGRABitmap tutorial 3/es|Siguiente tutorial (dibujando con el ratón)]]
+
[[BGRABitmap tutorial/es|Ir al Primer tutorial]] | [[BGRABitmap tutorial 3/es|Ir al Siguiente tutorial (dibujando con el ratón)]]
  
 
[[Category:Graphics]]
 
[[Category:Graphics]]

Revision as of 18:13, 15 April 2011

Deutsch (de) English (en) español (es) français (fr) русский (ru)


Home | Tutorial 1 | Tutorial 2 | Tutorial 3 | Tutorial 4 | Tutorial 5 | Tutorial 6 | Tutorial 7 | Tutorial 8 | Tutorial 9 | Tutorial 10 | Tutorial 11 | Tutorial 12 | Edit

Este tutorial muestra como cargar una imágen y dibujarla en una forma.

Crear un nuevo proyecto

Crea un nuevo proyecto y agrega referencias a BGRABitmap de la misma forma que se explica en el primer tutorial

Carga el bitmap

Copia una imagen al directorio de tu proyecto. Supongamos que su nombre es image.png.

Agrega una variable privada a la forma principal para guardar la imágen:

<delphi> TForm1 = class(TForm)

 private
   { private declarations }
   image: TBGRABitmap;
 public
   { public declarations }
 end; </delphi>

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 :

<delphi>procedure TForm1.FormCreate(Sender: TObject); begin

 image := TBGRABitmap.Create('image.png');

end; </delphi>

Dibuja el bitmap

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 :

<delphi>procedure TForm1.FormPaint(Sender: TObject); begin

 image.Draw(Canvas,0,0,True);

end; </delphi>

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.

Código

Finalmente deberías obtener algo como esto :

<delphi>unit UMain;

{$mode objfpc}{$H+}

interface

uses

 Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
 BGRABitmap, BGRABitmapTypes;

type

 { TForm1 }
 TForm1 = class(TForm)
   procedure FormCreate(Sender: TObject);
   procedure FormPaint(Sender: TObject);
 private
   { private declarations }
   image: TBGRABitmap;
 public
   { public declarations }
 end; 

var

 Form1: TForm1; 

implementation

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject); begin

 image := TBGRABitmap.Create('image.png');

end;

procedure TForm1.FormPaint(Sender: TObject); begin

 image.Draw(Canvas,0,0,True);

end;

initialization

 {$I UMain.lrs}

end.</delphi>

Ejecuta el programa

Deberías ver una forma con una imágen dibujada en ella en la esquina superior-izquierda.

BGRATutorial2.png

Centrar la imágen

Quizás quieras centrar la imágen en la forma. Para hacer esto, modifica el procedimiento FormPaint :

<delphi>procedure TForm1.FormPaint(Sender: TObject); var ImagePos: TPoint; begin

 ImagePos := Point( (ClientWidth - Image.Width) div 2,
                    (ClientHeight - Image.Height) div 2 );
 // test de posición negativa
 if ImagePos.X < 0 then ImagePos.X := 0;
 if ImagePos.Y < 0 then ImagePos.Y := 0;
 image.Draw(Canvas,ImagePos.X,ImagePos.Y,True);

end;</delphi>

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.

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.

Puedes ejecutar el programa y ver si funciona. Fijate que sucede si removes el test de posición negativa.

Estirar la imágen

Para estirar la imágen, necesitamos crear una imágen temporalmente estirada :

<delphi>procedure TForm1.FormPaint(Sender: TObject); var stretched: TBGRABitmap; begin

 stretched := image.Resample(ClientWidth, ClientHeight) as TBGRABitmap;
 stretched.Draw(Canvas,0,0,True);
 stretched.Free;

end;</delphi>

Por defecto, esto usa el estiramiento fino, pero puedes precisar si quieres usar el estiramiento simple (mas rápido) :

<delphi>stretched := image.Resample(ClientWidth, ClientHeight, rmSimpleStretch) as TBGRABitmap;</delphi>

Ir al Primer tutorial | Ir al Siguiente tutorial (dibujando con el ratón)