{
Article: Pictures inside a database
http://delphi.about.com/library/weekly/aa030601a.htm
Chapter three of the free
Delphi Database Course for beginners.
Displaying images (BMP, JPEG, ...) inside
an Access database with ADO and Delphi.
For the .zip file of this project click here.
}
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:\!Gajba\About\aboutdelphi.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:\!gajba\cdcovers\cdcover1.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.
{
********************************************
Zarko Gajic, BSCS
About.com Guide to Delphi Programming
http://delphi.about.com
email: delphi@aboutguide.com
free newsletter: http://delphi.about.com/library/blnewsletter.htm
forum: http://forums.about.com/ab-delphi/start/
********************************************
}