Como trabalhar com imagens (streams) sem criar arquivos temporários.
Boa tarde pessoal, o código abaixo está no evento DrawColumnCell do Dbgrid,
e serve para pegar a foto (jpg) do usuário do banco de dados (BLOB) e pôr na célula
do DBgrid.
O problema é que eu tenho que criar fotos temporárias no HD e removê-las depois.
Existe alguma maneira de eu ler o stream (jpg), criar um bmp e inserir a imagem
no DBGrid sem precisar criar arquivos e excluir depois?
Segue abaixo o código que estou utilizando. Conto com a ajuda de vocês.
Estou trabalhando com o Delphi 2007.
Abs.
Márcio
e serve para pegar a foto (jpg) do usuário do banco de dados (BLOB) e pôr na célula
do DBgrid.
O problema é que eu tenho que criar fotos temporárias no HD e removê-las depois.
Existe alguma maneira de eu ler o stream (jpg), criar um bmp e inserir a imagem
no DBGrid sem precisar criar arquivos e excluir depois?
Segue abaixo o código que estou utilizando. Conto com a ajuda de vocês.
Estou trabalhando com o Delphi 2007.
Abs.
Márcio
procedure TFrmDadosMarcacaoPesquisar.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
bitmap1 : TBitmap;
fixRect : TRect;
bmpWidth : integer;
StreamBlobFoto1 : TStream;
begin
with DataSourceResultado.DataSet do
begin
StreamBlobFoto1 := CreateBlobStream(FieldByName('FOTO1'),bmRead) ;
end;
try
StreamBlobFoto1.Seek(0, soFromBeginning);
with TFileStream.Create('d:\setor\foto1.jpg', fmCreate) do
try
CopyFrom(StreamBlobFoto1, StreamBlobFoto1.Size)
finally
Free
end;
finally
StreamBlobFoto1.Free
end;
if Trim(Column.Title.Caption) = 'Foto1' then
begin
bitmap1 := TBitmap.Create;
try
ConverterJpegParaBmp('d:\setor\foto1.jpg');
bitmap1.LoadFromFile('d:\setor\foto1.bmp');
//Fix the bitmap dimensions
bmpWidth := (Rect.Bottom - Rect.Top);
fixRect.Right := Rect.Left + bmpWidth;
//draw the bitmap
DBGrid1.Canvas.StretchDraw(fixRect,bitmap1);
finally
bitmap1.Free;
end;
end;
deletefile('d:\setor\foto1.jpg');
deletefile('d:\setor\foto1.bmp');
end;
Márcio Santos
Curtidas 0
Respostas
José Araújo
07/07/2011
Boa Tarde!
Fiz um código rápido aqui, e funcionou file, testa ai.
Declarar uses jpeg;
Procedure Seila;
var
jpg: TJPEGImage;
Stream: TMemoryStream;
begin
if DataSetIMAGEM.IsNull then
Exit;
//
jpg := TJPEGImage.Create;
try
Stream := TMemoryStream.Create;
try
DataSetIMAGEM.SaveToStream( Stream );
Stream.Position := 0;
jpg.LoadFromStream( Stream );
finally
Stream.Free;
end;
//
// *** Aqui é o teu Código ****
//
//Fix the bitmap dimensions
bmpWidth := (Rect.Bottom - Rect.Top);
fixRect.Right := Rect.Left + bmpWidth;
//draw the bitmap
DBGrid1.Canvas.StretchDraw(fixRect, jpg);
finally
jpg.Free;
end;
Fiz um código rápido aqui, e funcionou file, testa ai.
Declarar uses jpeg;
Procedure Seila;
var
jpg: TJPEGImage;
Stream: TMemoryStream;
begin
if DataSetIMAGEM.IsNull then
Exit;
//
jpg := TJPEGImage.Create;
try
Stream := TMemoryStream.Create;
try
DataSetIMAGEM.SaveToStream( Stream );
Stream.Position := 0;
jpg.LoadFromStream( Stream );
finally
Stream.Free;
end;
//
// *** Aqui é o teu Código ****
//
//Fix the bitmap dimensions
bmpWidth := (Rect.Bottom - Rect.Top);
fixRect.Right := Rect.Left + bmpWidth;
//draw the bitmap
DBGrid1.Canvas.StretchDraw(fixRect, jpg);
finally
jpg.Free;
end;
GOSTEI 0
Márcio Santos
07/07/2011
Funcionando...
Valeu mesmo... Grato pela Atenção.
Valeu mesmo... Grato pela Atenção.
GOSTEI 0