PDA

Ver la Versión Completa : Cargar imagenes desde una base de datos a un TImageList


camiz
18-04-2014, 21:46:29
hola amigos del foro, tengo un problemita quiero hacer un formulario que sirva para mostrar muchas imágenes. Que servirán para seleccionar y asignarle dicha imagen a un registro. ahora las imágenes que quiero mostrar están en bd, quiero mostrarlo por medio de un TListView, pero para eso nesecito cargar las imágenes a un TImageList para vincularlo al TListView.

ahora me gustaría saber si es posible cargar imágenes desde una base de datos a un TImageList o tengo que hacerlo de otra manera o existe un componente que haga este trabajo.

por el momento estoy tratando con este código pero no funciona.

var
S: TStream;
Graphic: TGraphic;
begin
with IMG do
begin
SQL.Clear;
SQL.Text := 'select * from img order by idimg asc';
Active := True;

First;
While Not Eof Do
begin

S := Oficios.CreateBlobStream(Oficios.FieldByName('IMAGEN'), bmRead);
try
Graphic := TPNGImage.Create;
try
Graphic.LoadFromStream(S);
IMGList_48.Items.Add.Assign(Graphic);
Finally
Graphic.Free;
end;
Finally
S.free;
end;


Next;
end;
Active := False;
end;

espero su ayuda.

ecfisa
18-04-2014, 23:10:01
Hola camiz.

Un ejemplo:

uses pngimage;

procedure TForm1.btnLoadPngClick(Sender: TObject);
var
i: Integer;
it: TListItem;
B: TBitmap;
P: TPNGObject;
begin
IBQuery1.First;
for i:= 0 to 30 do
begin
it:= ListView1.Items.Add;
it.ImageIndex:= i;
P:= TPNGObject.Create;
B:= TBitmap.Create;
try
P.Assign(TBlobField(IBQuery1.FieldByName('BANDERA')));
B.Width:= P.Width;
B.Height:= P.Height;
B.PixelFormat:= pf24bit;
P.Draw(B.Canvas, Rect(0, 0, P.Width, P.Height));
ImageList1.Add(B, B);
finally
P.Free;
B.Free;
end;
IBQuery1.Next;
end;
ListView1.LargeImages:= ImageList1;
end;


Salida:
http://sia1.subirimagenes.net/img/2014/04/18/140418111203344361.png

Saludos :)

camiz
19-04-2014, 01:15:17
ecfisa, me diste justo la respuesta correcta que necesitaba, gracias por tomarte el tiempo en responder mi inquietud.^\||/ Gracias amigo.

adaptandolo a mi proyecto quedo asi, por si acaso a alguien le hace falta.

var
i: Integer;
it: TListItem;
B: TBitmap;
P: TPNGObject;
begin
with Query do
begin
SQL.Clear;
SQL.Text := 'select * from img order by idimg asc';
Active := True;

First;
for i:= 0 to RecordCount -1 do
begin
it:= ListView1.Items.Add;
it.Caption := ' '+FieldByName('NOMBRE').AsString;
it.ImageIndex:= i;
P:= TPNGObject.Create;
B:= TBitmap.Create;
try
P.Assign(TBlobField(FieldByName('IMAGEN')));
B.Width:= P.Width;
B.Height:= P.Height;
B.PixelFormat:= pf24bit;
P.Draw(B.Canvas, Rect(0, 0, P.Width, P.Height));
ImageList1.Add(B, B);
finally
P.Free;
B.Free;
end;
Next;
end;
ListView1.LargeImages:= ImageList;
end;