Delphi database programming tutorial (5)

zhaozj2021-02-16  60

The second section uses dbiMage to lead JPEG-wrong way

Dbimage - Idea (THE DBIMAGE - Take One)

When trying to use Delphi to do new attempts, the first thing I did is to seek ways to Delphi's own help. This is the help documentation will answer: TDBIMAGE (Data Controls page at the Component Panel) represents a graphic image of a blob field currently recorded by the database. Use TDBIMAGE to represent graphic field values. TDBIMAGE allows the form to display graphical data for the database. TDBIMAGE is only more data visual properties than the TIMAGE component. Two of these most important properties are: DataSource and Field (fields). DataSource properties Connect the graphics component to the database. There is a DataSource component called DataSource1 on our form - represents a dataset. Field (field) property indicates the field with the image (in the table). Everything is clear, now place a DBImage component on the form and default to dbimage1. In order to realize DBImage with the BLOB field of the table, we only need to do the following configuration (using Object Inspector):

Dbimage1.datasource = DataSource1 Dbimage1.field = Picture

This is a necessary trick to display the JPEG image of the Picture field of the Applications table.

To verify that such configurations are working, the only thing we need is to set the ADOTABLE1 component's Active attribute to TRUE. We can do in the Object Inspector when designing. Once you do this, the following dialogs will appear:

what? Why show "bitmap image is invalid"? We have JPEG pictures instead of BMP pictures - Is there anything here? Let us look back and see help.

By clicking on the help, it concludes that we have to use the TJPEGImage object in order to get JPG images in the database. In order to display pictures, we need to simply, unreviewed by image components. At the same time, we need to carry out images from the BLOB object with streams. Help Document Narration: We should use TadobLobStream to access or change the value of the BLOB or MEMO field in the ADO data set.

The third section is used to lead the JPEG-error

Pulling the JPEG - Take Two!

Since we can't do anything to do anything - remove it from the form and put a normal TIMAGE component (Additional page) named AdoImage. Unfortunately, the image component does not have any data visual properties, so a separate program is required to display pictures in the database tables it represent. The easiest way to complete this is: Place a button on the form, put all the program code in its OnClick event, the name of the button: "BtnshowImage".

To use AdoblobStream, help documentation recommends creating a TadoblobStream instance, read the graphics field from the data set with "stream", then release the BLOB stream. Some places in the middle, we will need to load JPEG images from the TadFromstream object from the Tadoblobstream method. Image (Image) Components' Picture (Image), Graphic property will be used for real storage and display images. Field object, what is it?

At this time, I assume that only a little bit of knowledge about the field object is more than enough for you. In the development of the Delphi database, one of the main objects is the TField object. Field components are non-visual objects that represent data set fields when running (or design). Tadotable (and other TDataSet subclasses) provide access to the Fields Editor (Field Editor) when designing the design. Fields Editor allows you to choose the field you want to include in the data set. More importantly, it creates a solid list of field components used in the application data set. To call the Fields Editor, you can double-click the Tadotable component. By default, the list of fields is empty. Click the Add button to open a dialog box listing the list of fields for the Applications table. By default, all fields are selected, and then OK is selected.

Delphi will give the default name of the field as follows: Table (Table) Name Field (field) name. This means that our picture field name is: adotable1Picture.

TadoblobStream's Create method Creates an instance to read or write a specified BLOB field object, here is the Adotable1Picture field.

We write the program code in the onClick event of the BTNShowImage button. This code will read pictures from the currently selected line of Picture fields. The source code is as follows:

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 us run this project. Of course, set the adotable1.Active property to true. After the form is displayed, click the button to appear below:

Hey, what? The code is 100% correct but why does not display the image! Remember "never give up, never surrender"! Let us go deep into the byte level to see what happened!

转载请注明原文地址:https://www.9cbs.com/read-24479.html

New Post(0)