Delphi Access JPEG file to SQL Server database [转]

xiaoxiao2021-03-06  39

Recently, the author writes a small student management system, you need to use the ADO control to access the SQL Server database picture, see the information found to operate the BMP file (the mass increase database size), but found ★ Eagletian ★ master translation ADO English Technical Documentation, involving key technologies for data inventory JPEG files, tested in Win98 SQL Server desktop version, good operation, is now disclosed in the source code:

Unit unit1;

Interface

Uses

Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs,

ExtCtrls, Dbctrls, Grids, DBGRIDS, DB, ADODB, JPEG, Stdctrls, DBTABLES

{Must use the USES JPEG unit, enable storage JPG file format}

Type

TFORM1 = Class (TFORM)

DataSource1: TDataSource;

Adoquery1: TadoQuery;

DBGRID1: TDBGRID;

DBNAVIGATOR1: TDBNAVIGATOR;

Image1: timage;

SaveButton: tbutton;

Showbutton: tbutton;

OpenDialog1: TopEndialog;

AdoQuery1ID: Tintegerfield;

Adoquery1Pic: TBLOBFIELD;

Procedure SaveButtonClick (Sender: TOBJECT);

Procedure ShowButtonClick (Sender: TOBJECT);

Procedure DBNAVIGATOR1CLICK (Sender: Tobject; Button: TnaviGateBTN);

Private

{Private Declarations}

public

{Public declarations}

END;

VAR

FORM1: TFORM1;

IMPLEMENTATION

{$ R * .dfm}

Function JPEGSTARTSINBLOB (Picfield: TBLOBFIELD): Integer;

VAR

Ghy: tadoblobstream;

Buffer: Word;

HX: String;

Begin

RESULT: = -1;

Ghy: = tadoblobstream.create (picfield, bmread);

Try

While (result = -1) and (ghy.position 1

Begin

Ghy.readbuffer (buffer, 1);

HX: = INTTOHEX (Buffer, 2);

IF HX = 'FF' THEN BEGIN

Ghy.readbuffer (buffer, 1);

HX: = INTTOHEX (Buffer, 2);

IF hx = 'd8' Then Result: = ghy.position - 2

Else if hx = 'ff' Then

ghy.position: = ghy.position-1;

End; // IF

End; // while

Finally

ghy.free

End; // Try

END;

Procedure TFORM1.SAVEBUTTONCLICK (Sender: TOBJECT);

VAR

PicStream: tadoblobstream;

Begin

Adoquery1.edit;

PicStream: = tadoblobstream.create (TBLOBFIELD (Adoquery1.fields [1]), BMWRITE); if Form1.Opendialog1.execute THEN

Begin

PICSTream.LoadFromFile (OpenDialog1.FileName);

PICSTream.position: = 0;

Adoquery1.edit;

TBLOBFIELD (AdoQuery1.fields [1]). LoadFromstream (PICSTREAM);

Adoquery1.post;

END;

END;

Procedure TFORM1.SHOWBUTTONCLICK (Sender: TOBJECT);

VAR

Ghy: tadoblobstream;

Pic: tjpegimage;

Begin

Ghy: = tadoblobstream.create (AdoQuery1PIC, BMREAD);

Try

Ghy.seek (JPEGSTARTSINBLOB (AdoQuery1Pic), SofromBeginning;

Pic: = tjpegimage.create;

Try

Pic.LoadFromstream (GHY);

Image1.picture.graphic: = Pic;

Finally

Pic.free;

END;

Finally

ghy.free

END;

END;

Procedure tForm1.dbnavigator1click (Sender: Tobject; Button: TnaviGateBTN);

Begin

IF button in [NBFirst, NbPrior, NBNext, NBLAST] THEN ShowButton.click;

END;

End.

If the BMP file to be stored in the database is stored, then the code changes to the BMP file format can be stored as follows.

Procedure TFORM1.SHOWBUTTONCLICK (Sender: TOBJECT);

VAR

Ghy: tadoblobstream;

Pic: Tbitmap;

Begin

Ghy: = tadoblobstream.create (AdoQuery1PIC, BMREAD);

Try

{Ghy.seek (JPEGSTARTSINBLOB (AdoQuery1Pic), SOFROMBEGINNING

Pic: = Tbitmap.create;

Try

Pic.LoadFromstream (GHY);

Image1.picture.graphic: = Pic;

Finally

Pic.free;

END;

Finally

ghy.free

END;

END;

The specific operations that have been used to access the JPEG file in the SQL Server database with Delphi.

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

New Post(0)