Delphi Image Access Alternative solution

Source: Internet
Author: User
Tags bmp image paradox database

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.