Link to home
Start Free TrialLog in
Avatar of HamidHossain
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.LoadFromFile('myfile.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
Avatar of Imthiyaz_ph
Imthiyaz_ph

By default, the Blob size for BDE is limited to 32K. You can increase the limit upto 999K thru BDE Administrator:
 - 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.

Avatar of HamidHossain

ASKER

Hi,

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(query1bitmapfield).LoadFromFile('myfile.bmp');
TGraphicField(query1bitmapfield).LoadFromFile('myfile.bmp');

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.FieldByName('bitmapfield')).LoadFromStream(fs);
    query1.Post;
  finally
    fs.Free;
  end;
end;

Regards, Geo
ASKER CERTIFIED SOLUTION
Avatar of Imthiyaz_ph
Imthiyaz_ph

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Using ADO the following works here:

uses dbtables;

// saving to DB
procedure TForm1.Button1Click(Sender: TObject);
var
  fs: TFileStream;
begin
  fs := TFileStream.Create('c:\room.bmp', fmOpenRead);
  try
    ADOTable1.Append; // or query1.Edit; for replacing the current record
    fs.Position := 0;
    TBlobField(ADOTable1.FieldByName('pic')).LoadFromStream(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.CreateBlobStream(ADOTable1.FieldByName('pic'), bmRead));
  try
    bs.Position := 0;
    Image1.Picture.Bitmap.LoadFromStream(bs);
  finally
    bs.Free;
  end;
end;

Regards, Geo