Recently, after seeing two articles on CSDN about "Accessing JPEG files to SQLSERVER in DELPHI", I felt that the method described in it is desirable, but it is quite time-consuming. I have a simpler operation method here and it is safe. Reliable, I dare not enjoy it alone, I am willing to publish it and share it with everyone. The test was passed in Delphi7.0+Win2000+SqlServer 2000 and runs well. Now the idea and source code are disclosed as follows:
Solution:
1. The key is to dynamically convert the open JPEG file into a Tbitmap object and display it in the Timage object;
2. Submit the displayed image to the database.
In this example, a test example table was established in SQLSERVER2000: exam(xm char(10), photo image);
Program source code:
unit SavePic;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtDlgs, ExtCtrls, DBCtrls, Grids, DBGrids, DB, ADODB, Buttons,
StdCtrls,Jpeg;
type
TForm1 = class(TForm)
SpeedButton1: TSpeedButton;
ADOConnection1: TADOConnection;
Table1: TADOTable;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
DBImage1: TDBImage;
Image1: TImage;
SpeedButton2: TSpeedButton;
OpenPictureDialog1: TOpenPictureDialog;
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
SpeedButton3: TSpeedButton;
PRocedure SpeedButton2Click(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton3Click(Sender: TObject);
Private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Implementation
{$R *.dfm}
procedure TForm1.SpeedButton2Click(Sender: TObject);
var
bmp1:TBitmap;
jpg1:TJpegImage;
Begin
OpenPictureDialog1.DefaultExt:=GraphicExtension(TJpegimage);
if OpenPictureDialog1.Execute then
Begin
bmp1:=TBitmap.Create;
jpg1:=TJpegImage.Create;
try
jpg1.LoadFromFile(OpenPictureDialog1.FileName);
bmp1.Assign(jpg1);
Image1.Picture.Bitmap.Assign(bmp1);
Finally
jpg1.Free;
bmp1.Free;
end;
end;
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
Begin
table1.Open;
table1.insert;
table1.fieldbyname('xm').asstring:=Edit1.Text;
table1.FieldByName('photo').Assign(Image1.Picture);
table1.post;
table1.Refresh;
end;
end.