DELPHI存取JPEG檔案到SQL Server資料庫

來源:互聯網
上載者:User

             DELPHI存取JPEG檔案到SQL Server資料庫
                  高紅岩(ghyghost)
近日筆者書寫一個小型的學生管理系統時,需要用到ADO控制項存取SQL Server資料庫圖片,查看資料發現基本都是針對BMP檔案進行操作(巨增資料庫大小),但發現了★eagletian★高手翻譯的ADO英文技術文檔,裡面涉及到了資料庫存取JPEG檔案的關鍵技術,在win98+sql server案頭版中測試通過,運行良好,現於源碼公開如下:
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      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.

如果資料庫中要儲存的是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資料庫中的具體操作已經敘述完畢。
我的網站:
http://delphijl.99898.com
QQ:123168091

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.