Solved

show an image stored in a database

Posted on 2003-11-27
12
262 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
12 Comments
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 125 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 125 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 125 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 125 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
searching for DavidI's "Mobile Summer School download" 3 128
LAN or WAN ? 11 81
Find and Replace Stream with 0s 8 56
Performance of SQL statement 37 95
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

943 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now