Buscar

Exibir jpeg de tabela access no delphi

Código

Pessoal depois de pedir muita ajuda, procurar, e ninguém dar uma luz, consegui encontrar em um site americano(http://delphi.about.com/library/code/ncaa030601a.htm) essa dica, segue o código abaixo.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Buttons, ExtCtrls, StdCtrls, Db, ADODB, Grids, DBCtrls, DBGrids;

type
  TForm1 = class(TForm)
    ADOTable1: TADOTable;
    DataSource1: TDataSource;
    btnShowImage: TButton;
    ADOImage: TImage;
    ADOTable1Name: TWideStringField;
    ADOTable1Description: TWideStringField;
    ADOTable1Author: TWideStringField;
    ADOTable1Type: TWideStringField;
    ADOTable1Size: TFloatField;
    ADOTable1Cost: TBCDField;
    ADOTable1DateUpl: TDateTimeField;
    ADOTable1Picture: TBlobField;
    DBGrid1: TDBGrid;
    procedure btnShowImageClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
     { Public declarations }
  end;


const
 JPEGstarts = 'FFD8';
 BMPstarts = '424D';  //BM

var
  Form1: TForm1;

implementation
uses jpeg;
 {$R *.DFM}

function JpegStartsInBlob (PicField:TBlobField):integer;
var
 bS     : TADOBlobStream;
 buffer : Word;
 hx     : string;
begin
 Result := -1;
 bS := TADOBlobStream.Create(PicField, bmRead);
 try
  while (Result = -1) and (bS.Position + 1 < bS.Size) do
  begin
   bS.ReadBuffer(buffer, 1);
   hx:=IntToHex(buffer, 2);
   if hx = 'FF' then begin
     bS.ReadBuffer(buffer, 1);
     hx:=IntToHex(buffer, 2);
     if hx = 'D8' then Result := bS.Position - 2
     else if hx = 'FF' then bS.Position := bS.Position-1;
   end; //if
  end; //while
 finally
  bS.Free
 end;  //try
end;

procedure TForm1.btnShowImageClick(Sender: TObject);
var
  bS  : TADOBlobStream;
  Pic : TJpegImage;
begin
  bS := TADOBlobStream.Create(AdoTable1Picture, bmRead);
  try
    bS.Seek(JpegStartsInBlob(AdoTable1Picture), soFromBeginning);
    Pic:=TJpegImage.Create;
    try
     Pic.LoadFromStream(bS);
     ADOImage.Picture.Graphic:=Pic;
    finally
     Pic.Free;
    end;
  finally
    bS.Free
  end;
end;


procedure TForm1.FormCreate(Sender: TObject);
var sDBPath, sCons: string;
begin
//change the sDBPath to point to your database!
sDBPath := 'c:!GajbaAboutaboutdelphi.mdb';
sCons := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + sDBPath + ';Persist Security Info=False';

ADOTable1.ConnectionString := sCons;
ADOTable1.TableName := 'Applications';
DataSource1.DataSet := ADOTable1;
DBGrid1.DataSource := DataSource1;

ADOTable1.Active:=True;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ADOTable1.Active:=False;
end;


//Extra!! save JPG to table
procedure SaveJpegToTable(Table: TADOTable; PicField:TBlobField; sPicPath: string);
{
Usage:

  SPicFileName := 'C:!gajbacdcoverscdcover1.jpg';
  SaveJpegToTable(ADOTable1, ADOTable1Picture, SPicFileName);
}

var
  fS  : TFileStream;
begin
  fs:=TFileStream.Create(sPicPath, fmOpenRead);
  try
   Table.Edit;
   PicField.LoadFromStream(fs);
   Table.Post;
  finally
   fs.Free;
  end;
end;


end.

Publicidade

Vote na dica




Quantidade de votos: 0 votos
Aceitação: 0%


Detalhes da dica

Categoria: Imagens
Adicionada dia: 02/03/07
Por: Éderson Cassiano Bologna
Visualizada: 3188 vezes

Planeta Delphi - Tudo sobre programação Delphi Planeta Delphi - www.planetadelphi.com.br - Todos os direitos reservados | Copyright 2001-2009