In the "Delphi Access image Complete Solution" in the article, the author provides a Delphi access JPEG, BMP image to the database solution, although it applies to access and SQL database, However, it does not apply to all databases (such as the Paradox database in the graphic image field can not use this method to access image data), the following will introduce Delphi using the Assign method to access JPEG, BMP images to the database of another solution to complement the improvement. Demo database structure and window interface design in the previous article, no longer repeat, the corresponding program code for the Unit is replaced as follows:
1. Selection and preservation of image data
procedure Tform1.selectimageClick(Sender: TObject); //选择图像
begin
if openpicturedialog1.Execute then
image1.Picture.LoadFromFile(openpicturedialog1.FileName );
end;
procedure Tform1.savetodbClick(Sender: TObject); //保存图像到数据库
var
ext:string;
begin
if image1.picture.Graphic <> nil then //避免image1中无图像保存出错
begin
adotable1.Edit ;
adotable1.FieldByName('myimage').Assign(image1.Picture.Graphic);
//以下记录保存到数据库的图像格式
ext:=extractfileext(openpicturedialog1.FileName ); //取出文件扩展名
if uppercase(ext) = '.BMP' THEN
adotable1.FieldByName('isbmp').VALUE := 1 //BMP型图像数据
ELSE IF (UPPERCASE(EXT) = '.JPEG') OR (UPPERCASE(EXT) = '.JPG') THEN
adotable1.FieldByName('isbmp').VALUE := 0; //JPEG型图像数据
ADOTABLE1.Post ;
end;
end;
2. Image data Reading and display
procedure Tform1.ADOTable1AfterScroll(DataSet: TDataSet); //ADOTable1的AfterScroll事件方法程序
var
jpegimage:tjpegimage;
begin
image1.Picture.Graphic :=nil;
//下边BMP、JPEG两种图像数据必需分别处理
if adotable1.fieldbyname('isbmp').Asstring = '1' then //BMP型图像数据
image1.Picture.bitmap.Assign(adotable1.fieldbyname('myimage'))
//上边语句中的bitmap不能为graphic,否则会出错
else if adotable1.fieldbyname('isbmp').asstring = '0' then //JPEG型图像数据
begin //begin2
jpegimage := tjpegimage.Create ; //通过jpegimage将图像显示在image1,否则会出错
try
jpegimage.Assign(adotable1.fieldbyname('myimage'));
image1.Picture.Graphic :=jpegimage;
finally
jpegimage.Free ;
end; //end try
end; //end begin2
end;
Note: Do not forget to add a JPEG unit reference to the uses statement in the Unit File Interface section.
The above program code runs through the Delphi6.0+sql (or access or Paradox) database.