Link to home
Start Free TrialLog in
Avatar of marlon_ric
marlon_ric

asked on

storing and reading jpeg,gif,image file on an SQLserver database

hi experts,

I'm trying to load a picture using delphi6 into a database which by the way a Microsoft SQL server database version 7. My problem is when I load a bitmap it respond with an error message of "Invalid blob length", unless the size of bitmap is very small. This lead me to try loading a jpeg instead but it only respond with a different error messages "JPEG error #52" and "bitmap image is not valid".

I sincerely hope you can advice me on this because its very important to my job. I'll give this question 250 points. Thank you very much.

My code is as follows:

  Image1.Picture.LoadFromFile('c:\my_picture.jpg');
    with Table1 do
    begin
    insert;
    Table1pic.Assign(Image1.Picture.graphic);
    Post;
  end;

and also try

  with Table1 do
  begin
    insert;
    j:= TJPEGImage.Create;
    j.LoadFromFile(OpenDialog1.FileName);
    Table1pic.Assign(j);
    Post;
  end;

and last

  with Table1 do
  begin
    insert;
    fs := TFileStream.Create(OpenDialog1.filename, fmOpenRead);
    bs:= TBlobStream.create(Table1Pic, bmWrite);
    bs.CopyFrom(fs, 0);
    bs.Free;
    fs.Free;
    post;
  end;
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

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
Avatar of marlon_ric
marlon_ric

ASKER

thanks! I tried

with Table1 do
  begin
  insert;
  TBlobField(table1Pic).LoadFromFile(OpenDialog1.filename);
  post
end;

but still it gives the error "bitmap image is not valid".

I removed "ftblob" from your
  "TBlobField(table1Pic).LoadFromFile('Filename',ftBlob);"
because it generates compiler error of "too many actual parameter"

marlon.
>because it generates compiler error of "too many actual parameter"
which delphi version do you have?

>but still it gives the error "bitmap image is not valid".
a) your database-table-fieldtype should be a blob-type,
rather than a graphic-type

b) you can't use a tdbimage for displaying a jpeg-image, use a timage instead

meikl ;-)
1)im using delphi version 6.
2)actually the datatype that I use for this field is "Image type" on SQLServer. But I guess this is the only true blob field for the images data on this database because I try to change it into a "varBinary" and it complains on error "type mismatch"
3)yes, im using TImage.
1)im using delphi version 6.
2)actually the datatype that I use for this field is "Image type" on SQLServer. But I guess this is the only true blob field for the images data on this database because I try to change it into a "varBinary" and it complains on error "type mismatch"
3)yes, im using TImage.
do you use ado, bde or other
for connecting to the database?
I use bde which connect through odbc.
did you thought to use ado instead of bde?

anyway it should work,
if you have persistent fields,
then delete it and try again,
after restructuring your table field
to varbinary

meikl ;-)
hi again,

I dont know how but the code you gave me above suddenly work.

I save it into the database as

  with Table1 do
    begin
    insert;
    Table1PatientId.asString:= 'marlon';
    TBlobField(table1Pic).LoadFromFile  (OpenDialog1.filename);
    Post;
  end;

I read it as

  bs:= TBlobStream.create(Table1Pic, bmRead);
  j:= TJPEGImage.Create;
  j.loadfromStream(bs);
  Image1.Picture.graphic:= j;

and it works thank you very much kretzschmar.

One last thing, how can I use GIF, delphi doesn't seem to have a TGIFImage for GIF, is there any class that encapsulate it just like TJPEGImage for JPEG?
One last thing, how can I use GIF, delphi doesn't seem to have a TGIFImage for GIF, is there any class that encapsulate it just like TJPEGImage for JPEG?
glad you got it work :-))

there is a good tgifimage from melander,
download here

http://www.torry.net/vcl/graphics/gif/gifimage.exe

but don't know if it will work with d6,
just try

meikl ;-)