Sam80
asked on
How to save the image into the database in Delphi7?
Hi ,
the function I want is to save the picture in Timage object to the sql database, I have tried several ways to do it ,but not successful.
the picture I load from file may be jpg ,bmp or emf,wmf.
I look up some references , most of them told me to do like this below:
MS:TmemoryStream;
pic:TGraphic.
image1.picture.bitmap.load fromfile(f ilename);
//or pic:=Tgraphic.create;
// pic.loadfromfile(filename)
image1.picture.bitmap.save tostream(m s);
ms.position:=0;
table1.open;
table1.insert; // or append;
TBolbfield(table1.fieldbyn ame('image ')).loadfr omstream(m s);
// or table1.fieldbyname('image' ).assign(p ic);
table1.post;
table1.close;
ms.free;
pic.free;
it works if the picture I load fromis the bitmap file,otherwise If the image is loaded from a jpg file, the exception "bitmap is not valid" comes out;
I have also tried to use TGraphic to load the file, it even failed when I try to load a jpg file in it.
I am so confused now ,pls tell me what to do .My enviroment"
win2k pro, delphi7. sqlserver 2000;
Regards,
Sam
the function I want is to save the picture in Timage object to the sql database, I have tried several ways to do it ,but not successful.
the picture I load from file may be jpg ,bmp or emf,wmf.
I look up some references , most of them told me to do like this below:
MS:TmemoryStream;
pic:TGraphic.
image1.picture.bitmap.load
//or pic:=Tgraphic.create;
// pic.loadfromfile(filename)
image1.picture.bitmap.save
ms.position:=0;
table1.open;
table1.insert; // or append;
TBolbfield(table1.fieldbyn
// or table1.fieldbyname('image'
table1.post;
table1.close;
ms.free;
pic.free;
it works if the picture I load fromis the bitmap file,otherwise If the image is loaded from a jpg file, the exception "bitmap is not valid" comes out;
I have also tried to use TGraphic to load the file, it even failed when I try to load a jpg file in it.
I am so confused now ,pls tell me what to do .My enviroment"
win2k pro, delphi7. sqlserver 2000;
Regards,
Sam
I think it's better:
var
F:TFileStream;
begin
F:=TFileStream.create('You rFileNameH ere',fmOpe nRead);
//...
TBolbfield(table1.fieldbyn ame('image ')).loadfr omstream(F );
//...
F.free;
var
F:TFileStream;
begin
F:=TFileStream.create('You
//...
TBolbfield(table1.fieldbyn
//...
F.free;
ASKER
Thank you for your suggestion.
I wanna load both jpeg and bitmap file , further more , load the file in the openpicturedialg supported format(emf,wmf), not just a single format and neednt specify different way to load the different format file . I will try the TFileStream later.
Thank you all .
Regards,
Sam
I wanna load both jpeg and bitmap file , further more , load the file in the openpicturedialg supported format(emf,wmf), not just a single format and neednt specify different way to load the different format file . I will try the TFileStream later.
Thank you all .
Regards,
Sam
Hi,
Just add jpeg to your uses clause and keep using TImage. This works fine here.
Regards, Geo
Just add jpeg to your uses clause and keep using TImage. This works fine here.
Regards, Geo
ASKER
I have use Tbitmap to load the picture in the TImage. And then use table.fieldbyna,e('image') .assign(bm p) to save the img to the database.
but I now doubt there will be a mess data in the database for all the image format is bitmap.
================
To Geo,
In delphi7 , I dont have to add the jpeg unit and use Tjpegimage as well. but if i add that unit, delphi report that there has no such unit.
To delphized,
I have try your method , but it's so strange that I read the jpeg file into a memorystream type object and save it into the field like the way you show to me , but in that step , the exception message "bitmap is not valid " still comes out,in contrast , if i save a bitmap memorystream, it works !! . I doubt it's the database's problem . in my sql, I define the field "image" as image type, length is 16. why it can verify my memorystream is bitmap or not??
To all ,
The another problem I meet is the Tgraphic problem. I cannot use Tgraphic.loadfromfile because of the error "abstract error" . I have tried to do : gra:Tgraphic;
gra:=Tgraphic.create;
gra:=Timage.picture.Graphi c; // not gra.assign(..)
in this way, the gra has the picture's width and height same as that in Timage !
Regards,
Sam
but I now doubt there will be a mess data in the database for all the image format is bitmap.
================
To Geo,
In delphi7 , I dont have to add the jpeg unit and use Tjpegimage as well. but if i add that unit, delphi report that there has no such unit.
To delphized,
I have try your method , but it's so strange that I read the jpeg file into a memorystream type object and save it into the field like the way you show to me , but in that step , the exception message "bitmap is not valid " still comes out,in contrast , if i save a bitmap memorystream, it works !! . I doubt it's the database's problem . in my sql, I define the field "image" as image type, length is 16. why it can verify my memorystream is bitmap or not??
To all ,
The another problem I meet is the Tgraphic problem. I cannot use Tgraphic.loadfromfile because of the error "abstract error" . I have tried to do : gra:Tgraphic;
gra:=Tgraphic.create;
gra:=Timage.picture.Graphi
in this way, the gra has the picture's width and height same as that in Timage !
Regards,
Sam
I told you TFileStream... and I didn't know you were usin a Image field (Ithought a blob field).
You can't use the TGraphic method because TGraphic is a abstract class that is derived in different subclasses for every king of image (bitmab,jpeg, ...).
After having known this last information, you could make this way;
var
bmp:TBitmap;
begin
bmp:=TBitmap.create;
bmp.height:=Image1.picture .height;
bmp.width:=Image1.picture. width;
bmp.canvas.draw(0,0,Image1 .picture);
and then assign the bmp object to your field
bmp.free;
end;
You can't use the TGraphic method because TGraphic is a abstract class that is derived in different subclasses for every king of image (bitmab,jpeg, ...).
After having known this last information, you could make this way;
var
bmp:TBitmap;
begin
bmp:=TBitmap.create;
bmp.height:=Image1.picture
bmp.width:=Image1.picture.
bmp.canvas.draw(0,0,Image1
and then assign the bmp object to your field
bmp.free;
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
TImage does not support JPEG files.
Use TJpegImage for image1.
i.e.
Var
MyImg : TJpegImage;
Begin
...
MyImg := TJpegImage.Cleate;
MyImg.LoadFromFile('MyImg.
MyImg.SaveToStream(MS);
...
*-*-*-*-*
Also, remember to add jpeg in uses .
HTH,
...Shu