Solved

show an image stored in a database

Posted on 2003-11-27
12
260 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:
mnasman 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

21 Experts available now in Live!

Get 1:1 Help Now