HamidHossain
asked on
How to Store a Bitmap Images into MSSQL field from Delphi
Hi,
I am using Delphi 7 with MS-SQL 2000 database. I want to use BDE or ADO connection to store a bitmap into a database field. My bitmap file size is usually 200kb.
I created a field as IMAGE type in database. and I used the following to insert my bitmap into it:
query1bitmapfield.LoadFrom File('myfi le.bmp');
//query1bitmapfield is a TBlobField
The previous works fine with bitmaps smaller that 32kb size.
Is there any solution to store my 200kb images into a database field? and how to do this?
what field type is suggested to be used?
Regards,
Hamid Hossain
I am using Delphi 7 with MS-SQL 2000 database. I want to use BDE or ADO connection to store a bitmap into a database field. My bitmap file size is usually 200kb.
I created a field as IMAGE type in database. and I used the following to insert my bitmap into it:
query1bitmapfield.LoadFrom
//query1bitmapfield is a TBlobField
The previous works fine with bitmaps smaller that 32kb size.
Is there any solution to store my 200kb images into a database field? and how to do this?
what field type is suggested to be used?
Regards,
Hamid Hossain
ASKER
Hi,
I tried using ADO but I still have the problem.
Tell me more about it or show me some code
Regards,
Hamid
I tried using ADO but I still have the problem.
Tell me more about it or show me some code
Regards,
Hamid
Hi,
First try to explicitly typecast the field as blob or graphic field:
TBlobField(query1bitmapfie ld).LoadFr omFile('my file.bmp') ;
TGraphicField(query1bitmap field).Loa dFromFile( 'myfile.bm p');
Or use streams:
var
fs: TFileStream;
begin
fs := TFileStream.Create('myfile .bmp', fmOpenRead);
try
query1.Append; // or query1.Edit; for replacing the current record
fs.Position := 0;
TGraphicField(query1.Field ByName('bi tmapfield' )).LoadFro mStream(fs );
query1.Post;
finally
fs.Free;
end;
end;
Regards, Geo
First try to explicitly typecast the field as blob or graphic field:
TBlobField(query1bitmapfie
TGraphicField(query1bitmap
Or use streams:
var
fs: TFileStream;
begin
fs := TFileStream.Create('myfile
try
query1.Append; // or query1.Edit; for replacing the current record
fs.Position := 0;
TGraphicField(query1.Field
query1.Post;
finally
fs.Free;
end;
end;
Regards, Geo
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Using ADO the following works here:
uses dbtables;
// saving to DB
procedure TForm1.Button1Click(Sender : TObject);
var
fs: TFileStream;
begin
fs := TFileStream.Create('c:\roo m.bmp', fmOpenRead);
try
ADOTable1.Append; // or query1.Edit; for replacing the current record
fs.Position := 0;
TBlobField(ADOTable1.Field ByName('pi c')).LoadF romStream( fs);
ADOTable1.Post;
finally
fs.Free;
end;
end;
// loading from DB to an Image
procedure TForm1.Button2Click(Sender : TObject);
var
bs: TBlobStream;
begin
bs := TBlobStream(ADOTable1.Crea teBlobStre am(ADOTabl e1.FieldBy Name('pic' ), bmRead));
try
bs.Position := 0;
Image1.Picture.Bitmap.Load FromStream (bs);
finally
bs.Free;
end;
end;
Regards, Geo
uses dbtables;
// saving to DB
procedure TForm1.Button1Click(Sender
var
fs: TFileStream;
begin
fs := TFileStream.Create('c:\roo
try
ADOTable1.Append; // or query1.Edit; for replacing the current record
fs.Position := 0;
TBlobField(ADOTable1.Field
ADOTable1.Post;
finally
fs.Free;
end;
end;
// loading from DB to an Image
procedure TForm1.Button2Click(Sender
var
bs: TBlobStream;
begin
bs := TBlobStream(ADOTable1.Crea
try
bs.Position := 0;
Image1.Picture.Bitmap.Load
finally
bs.Free;
end;
end;
Regards, Geo
- Open BDE Administrator
- Under Configuration tab, select Configuration -> Drivers -> Native -> MSSQL from the treeview.
- Change the Blob Size to 999 and apply the changes (Ctrl + A)
You also need to change the Blob Size for each existing aliases that has been defined with MSSQL.
Unlike BDE, I havnt come up with any Blob size limitations with ADO. I have saved images more than 1MB into the DB using ADO.
The 'Image' datatype should be used for storing images into a table.