?
Solved

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

Posted on 2003-03-17
11
Medium Priority
?
277 Views
Last Modified: 2013-11-19
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;
0
Comment
Question by:marlon_ric
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 1000 total points
ID: 8156805
try

TBlobField(table1Pic).LoadFromFile('Filename',ftBlob);

meikl ;-)
0
 

Author Comment

by:marlon_ric
ID: 8156929
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.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8156939
>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 ;-)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:marlon_ric
ID: 8157163
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.
0
 

Author Comment

by:marlon_ric
ID: 8157249
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.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8157274
do you use ado, bde or other
for connecting to the database?
0
 

Author Comment

by:marlon_ric
ID: 8157356
I use bde which connect through odbc.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8157570
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 ;-)
0
 

Author Comment

by:marlon_ric
ID: 8157737
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?
0
 

Author Comment

by:marlon_ric
ID: 8157757
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?
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8157826
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 ;-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
In addition to being a great web-based presentation tool, Prezi also makes it easy to save your presentation as a PDF to share with others as well. Learn how in this tutorial. Select the share icon from the top menu in your Prezi editor: Select "D…
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question