1. Home
  2. Computing & Technology
  3. Delphi Programming
Pictures inside a database
Page 3: Streaming the Jpeg - the wrong way.
 More of this Feature
• Page 1: BLOBs in Access
• Page 2: DBImage.NOT
• Page 4: JPG SOI marker
• Page 5: Project's Code
 Join the Discussion
"Post your views and comments to this chapter of the free Delphi database Programming Course"
Discuss!
  Related Resources
• free DB Course.TOC
• Delphi DB articles
 

  Pulling the Jpeg - take two!
Since we can do nothing with the DBImage - remove it from the form and place an ordinary TImage component (Additional palette) on it. Name it ADOImage. Unfortunately the Image component does not have any data-aware properties, so all the code needed to show a picture from a table inside it will require a separate procedure. The easiest thing to do is to put a Button on a form and place all the code inside it's OnClick event. Name the button 'btnShowImage'.

To use the ADOBLOBStream the Help suggests to create an instance of TADOBlobStream, use the methods of the stream to read from a graphic field in a dataset, and then free the BLOB stream. Somewhere "in the middle" we'll need to use LoadFromStream to load a Jpeg image from a TADOBlobStream object. The Image's component Picture.Graphic property will be used to actually store and display the picture.

Field object, what are those?
At this moment I'll assume that only a small amount of knowledge on Field objects will be enough for you to keep with this chapter. In Delphi database development one of the primary objects is the TField object. Field components are non-visual objects that represent fields of the dataset at run (and design) time. The TADOTable (and other TDataSet descendant) gives access to the Fields Editor at design time. The Fields Editor enables you to select the fields that you want to include in the dataset. More important, it creates a persistent lists of the field components used by the dataset in your application. To invoke the Fields Editor double click the TADOTable component. By default, the list of fields is empty. Click Add to open a dialog box listing the fields in the Applications table. By default, all fields are selected. Select OK.
When Delphi gives (default) names to Fields the following notation is used: Table name + Field name. This means that our picture field has the name: ADOTable1Picture.

The TADOBlobStream Create method creates an instance of TADOBlobStream for reading from or writing to a specific BLOB field object, which is in our case the ADOTable1Picture field.

We will place the code in the OnClick event for a btnShowImage button. The code should read the picture from the Picture field of the currently selected row. This is how the code should look like:


uses jpeg;
...
procedure TForm1.btnShowImageClick(Sender: TObject);
var
  bS  : TADOBlobStream;
  Pic : TJpegImage;
begin
  bS := TADOBlobStream.Create
        (AdoTable1Picture, bmRead);
  try
    Pic:=TJpegImage.Create;
    try
     Pic.LoadFromStream(bS);
     ADOImage.Picture.Graphic:=Pic;
    finally
     Pic.Free;
    end;
  finally
    bS.Free
  end;
end;

Ok, let's run the project now. Of course set the ADOTable1.Active property to True. The form is displayed and after clicking on a button this is what we got:

Jpeg read error

Hm, what now? The code in the procedure is 100% correct but the image doesn't get displayed! Remember the "Never give up, never surrender"? Let's go down to byte level to see what's happening!

Next page > Seeking the start of Jpeg in the BLOB - the correct way > Page 1, 2, 3, 4, 5

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.