Solved

show an image stored in a database

Posted on 2003-11-27
12
266 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 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
Technology Partners: 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 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 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