Delphi accesses JPEG files to the SQL Server database

Source: Internet
Author: User

Delphi accesses JPEG files to the SQL Server database
Ghyghost)
Recently, when I wrote a small student management system, I needed to use the ADO control to access the image of the SQL Server database. I found that most of the operations were performed on BMP files (greatly increasing the database size), but I found★Eagletian★The ADO English technical document translated by experts involves the key technologies used to access JPEG files in databases. It has been tested in WIN98 + SQL Server Desktop edition and runs well. The source code is published as follows:
Unit unit1;

Interface

Uses
Windows, messages, sysutils, classes, graphics, controls, forms, dialogs,
Extctrls, dbctrls, grids, dbgrids, DB, ADODB, JPEG, stdctrls, dbtables;
{The uses JPEG unit is required to enable the storage of JPG files}
Type
Tform1 = Class (tform)
Performance1: 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 compute startsinblob (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.exe cute 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: Tsung image;
Begin
Ghy: = tadoblobstream. Create (adoquery1pic, bmread );
Try
Ghy. Seek (export startsinblob (adoquery1pic), sofrombeginning );
PIC: = tsf-image. 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 is to be stored in the database, you can use procedure tform1.showbuttonclick (Sender: tobject). When the code is changed as follows, you can store the operation that displays the BMP file format.
Procedure tform1.showbuttonclick (Sender: tobject );
VaR
Ghy: tadoblobstream;
PIC: tbitmap;
Begin
Ghy: = tadoblobstream. Create (adoquery1pic, bmread );
Try
{Ghy. Seek (export startsinblob (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 for using Delphi to access JPEG files to the SQL Server database have been described.
My Website:
Http://delphijl.99898.com
QQ: 123168091

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.