Solved

show an image stored in a database

Posted on 2003-11-27
12
263 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
build html 5 client that connect to indy Tidtcpserver 3 395
Best Firemonkey component pack 1 104
Help on project with Soap 10 53
Firemonkey allowing RTL on android 6 31
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

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