procedure TForm1.selectimageClick(Sender: TObject); //选择图像 begin if openpicturedialog1.Execute then image1.Picture.LoadFromFile(openpicturedialog1.FileName ); end;
////如下保存方法only to sql and access’data procedure TForm1.savetodbClick(Sender: TObject); //保存图像 var strm:tmemorystream; ext:string; begin if image1.picture.Graphic <> nil then //避免image1中无图像保存出错 begin ext:=extractfileext(openpicturedialog1.FileName ); strm := tmemorystream.Create ; try image1.Picture.Graphic.SaveToStream(strm); adotable1.Edit ; strm.Position :=0; DBImage1.dataField :=’’; //dbimage只能显示BMP,否则myimage由BMP变为jpeg时会出错 tblobfield(adotable1.FieldByName(’myimage’)).LoadFromStream(strm); //如需直接由文件保存 TBlobField(adotable1.FieldByName(’myimage’)).LoadFromFile(OpenPictureDialog1.FileName); //以下记录保存到数据库的图像格式 if uppercase(ext) = ’.BMP’ then begin adotable1.FieldByName(’isbmp’).Value := 1; dbimage1.dataField := ’myimage’; end else if (uppercase(ext) = ’.JPG’) OR ( uppercase(ext) = ’.JPEG’) THEN adotable1.FieldByName(’isbmp’).Value := 0; adotable1.Post ; finally strm.Free ; //如果你选用TBLOBSTREAM类,程序运行到此语句会出错,可该语句前添入adotable1.edit end; end; end; ///如下显示方法不适用于paradox中的graphic字段的显示。 procedure TForm1.adoTable1AfterScroll(DataSet: TDataSet); //显示图像 var strm:tadoblobstream; jpegimage:tjpegimage; bitmap:tbitmap; begin strm := tadoblobstream.Create(tblobfield(adotable1.fieldbyname(’MYIMAGE’)),bmread); try //try1 strm.position :=0; image1.Picture.Graphic := nil; DBIMAGE1.DataField := ’’; //显示时,BMP、JPEG两种图像数据必需分别处理 if adotable1.fieldbyname(’isbmp’).asstring =’1’ then begin //begin11 bitmap := tbitmap.Create ; try //try11 bitmap.LoadFromStream(strm); image1.Picture.Graphic := bitmap; DBIMAGE1.DataField := ’myimage’; finally bitmap.Free; end; //end try11 end //end begin11 else if adotable1.fieldbyname(’isbmp’).asstring =’0’ then begin //begin12 jpegimage := tjpegimage.Create ; try //try12 jpegimage.LoadFromStream(strm); image1.Picture.Graphic := jpegimage; finally jpegimage.Free ; end; //end try12 end; //end begin12 finally strm.Free ; end; //end try1 end;
////显示时必须分bmp and jpeg 两种情况处理,而保存可统一。
procedure TForm1.savetofileClick(Sender: TObject); var tmpstr:string; begin if image1.Picture.Graphic <> nil then