DELPHI存取JPEG文件到SQL Server數(shù)據(jù)庫
發(fā)表時間:2023-08-17 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]高紅巖(ghyghost)近日筆者書寫一個小型的學(xué)生管理系統(tǒng)時,需要用到ADO控件存取SQL Server數(shù)據(jù)庫圖片,查看資料發(fā)現(xiàn)基本都是針對BMP文件進行操作(巨增數(shù)據(jù)庫大小),但發(fā)現(xiàn)了★eagl...
高紅巖(ghyghost)
近日筆者書寫一個小型的學(xué)生管理系統(tǒng)時,需要用到ADO控件存取SQL Server數(shù)據(jù)庫圖片,查看資料發(fā)現(xiàn)基本都是針對BMP文件進行操作(巨增數(shù)據(jù)庫大小),但發(fā)現(xiàn)了★eagletian★高手翻譯的ADO英文技術(shù)文檔,里面涉及到了數(shù)據(jù)庫存取JPEG文件的關(guān)鍵技術(shù),在win98+sql server桌面版中測試通過,運行良好,現(xiàn)于源碼公開如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, DBCtrls, Grids, DBGrids, Db, ADODB,jpeg, StdCtrls,dbtables;
{一定要USES JPEG單元,使能存儲JPG文件格式}
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 < ghy.Size) do
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.
如果數(shù)據(jù)庫中要存儲的是BMP文件,則在procedure TForm1.showbuttonClick(Sender: TObject);過程中代碼更改如下即可存儲顯示BMP文件格式的操作。
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;
到此用DELPHI存取JPEG文件到SQL Server數(shù)據(jù)庫中的具體操作已經(jīng)敘述完畢。
我的網(wǎng)站:
http://delphijl.99898.com
QQ:123168091