?
Solved

show an image stored in a database

Posted on 2003-11-27
12
Medium Priority
?
267 Views
Last Modified: 2010-04-05
Hi friends

I have an interbase 6  database. One table has a blob field holding a picture and I need to show it.
The problem is that I don't know in wish format is stored this picture. I ran a routine to show a jpg picture and it failed, I got an JPEG ERROR #52.
How can I know the format of this picture and show it?

thanks in advance
0
Comment
Question by:ahll
[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
12 Comments
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 500 total points
ID: 9834194
Hello

 Take a look to this article
http://delphi.about.com/library/weekly/aa030601c.htm

0
 
LVL 7

Assisted Solution

by:jconde
jconde earned 500 total points
ID: 9834737
You must parse the first couple of bytes stored in the blob-field to know which type of picture-format is stored in the field.

If you *know* the picture is a JPEG, try the following code:

procedure TForm1.Button4Click(Sender: TObject);
var
  t : TBlobField;
  jpg: TJpegImage;
   x: TMemoryStream;
begin
  t := MyDataSet1.FieldByName('myblob') as TBlobField;
  X := TMemoryStream.Create;
  try
  t.SaveToStream(x);
  if (x.Size > 0) then
  begin
    jpg := TJpegImage.Create;
    x.Position := 0;
    try
    jpg.LoadFromStream(x);
    Image1.Picture.Assign(jpg);
    finally
    jpg.free;
    end;
  end;
  finally
  x.free;
  end;
end;

0
 
LVL 9

Assisted Solution

by:mocarts
mocarts earned 500 total points
ID: 9837193
hi, ahll
try this out:

uses jpeg;

function GetGraphicFromBlob(aField: TBlobField): TGraphic;
var
 s: TStream;
 buf: word;
begin
 assert(aField <> nil);
 Result := nil;
 s := aField.DataSet.CreateBlobStream(aField, bmRead);
 try
   if s.read(buf, sizeof(buf)) <> sizeOf(buf) then exit;
   s.Position := 0;
   if buf = $D8FF then
     Result := TJPEGImage.Create
   else if buf = $4D42 then
     Result := TBitmap.Create
   else if buf = $CDD7 then
     Result := TMetafile.Create
   else if buf = 0 then
     Result := TIcon.Create;
   if assigned(Result) then begin
     try
       Result.LoadFromStream(s);
     except
       Result.Free;
       Result := nil;
     end;
   end;
 finally
   s.Free;  // must free stream
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
begin
  i := 1;
  with Table1 do begin
    Open;
    try
      // if image will not load from blob then result of getGraphicFromBlob
      // will be nil and Image1 will be empty
     // you can of course check result to give an error to user by using intermediate var
      Image1.Picture.Assign(GetGraphicFromBlob(TBlobField(FieldByName('img'))));
    finally
      close;
    end;
  end;
end;

wbr, mo.
0
Independent Software Vendors: 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!

 
LVL 3

Author Comment

by:ahll
ID: 9852704
OK, I tried all this things and no results.
I saved the file to the HD and it doesn' t look like a jpg. I read the link posted by mnasman and it's very interesting but it didn't help me.
How I need this very much I'll increase my points on this question.

Thanks all
0
 
LVL 9

Expert Comment

by:mocarts
ID: 9856553
what is first two bytes of saved file (hex representation)?
0
 
LVL 3

Author Comment

by:ahll
ID: 9876631
all the files begin with diferents bytes, I saved some blob fields to the HD and they are all differents
0
 
LVL 3

Author Comment

by:ahll
ID: 9924534
please adm, can you delete this quiestion? I think nobody has an answer for my problem

thanks all anyway
0
 

Assisted Solution

by:ChrisLM
ChrisLM earned 500 total points
ID: 9947592
Hi,
    I don't have any experenice with interbase databases but you can try this. Instead of storing the file in the database, keep the file on the hard-drive and put the path to the image in the database. eg (C:\Images\xxx.bmp). You will need to change the blob
field to a string field. If you use the TDataSource component then put this code under the 'DataSourceDataChange' event:

   if not FileExists(Table.FieldByName('Image').AsString) then              
      Image.Picture := nil            
    else                                                                
      Image.Picture.LoadFromFile(Table.FieldByName('Image').AsString);

Table = 'Your Table Component'.
('Image') = 'The field that has the image path'.
Image = 'Picture Component'.

Chris.
0
 
LVL 3

Author Comment

by:ahll
ID: 9963969
Thanks Chris, but I have tha database full of data and I need to retrieve it.
0
 
LVL 3

Author Comment

by:ahll
ID: 10815452
I need to delete this quiestion, please

thanks
0

Featured Post

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!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

741 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