access数据库中怎么样对图片进行存储和读出,而不是存取路径等方法,希望能直接存到数据库内,并尽量减小数据库大小?

解决方案 »

  1.   

    用var
      strm:tmemorystream;
      ext:string;
    begin
        dm.materialcds.append;
    strm:=tmemorystream.Create;
        if image1.picture.Graphic <> nil then
        begin
           ext:=extractfileext(openpicturedialog1.FileName ); //取出文件的扩展名
           image1.Picture.Graphic.SaveToStream(strm);
           strm.Position :=0;
           tblobfield(dm.MaterialCDS.FieldByName('图片')).LoadFromStream(strm);
            if uppercase(ext) = '.BMP' then
            dm.MaterialCDS.FieldByName('isbmp').Value := 1
            else if (uppercase(ext) = '.JPG') OR ( uppercase(ext) = '.JPEG') Then
                dm.MaterialCDS.FieldByName('isbmp').Value := 0;
          end;
          dm.MaterialCDS.ApplyUpdates(0);
          strm.Free;
    end;
    读图片
      var
      strm:tadoblobstream;
      jpegimage:tjpegimage;
      bitmap:tbitmap;
    strm := tadoblobstream.Create(tblobfield(dm.severADOQ1.FieldByName('图片')),bmread);
       try //try1
        //strm.position :=0;
        xzcailiaoform.image1.Picture.Graphic := nil;
        if dm.MaterialCDS.FieldByName('isbmp').AsString ='1' then //BMP型图像数据
        begin //begin11
          bitmap := tbitmap.Create ;
          try //try11
            bitmap.LoadFromStream(strm);
            xzcailiaoform.image1.Picture.Graphic := bitmap;
          finally
            bitmap.Free;
          end; //end try11
          end
          else if dm.severADOQ1.FieldByName('isbmp').asstring ='0' then //JPEG型图像数据
          begin //begin12
            jpegimage := tjpegimage.Create ;
          try //try12
            jpegimage.LoadFromStream(strm);
            xzcailiaoform.image1.Picture.Graphic := jpegimage;
          finally
            jpegimage.Free ;
          end; //end try12
          end; //end begin12
          finally
          strm.Free ;
        end; //end try1
      

  2.   

    var
      PersonPictureStream,SheetPictureStream:TMemoryStream;
      PersonPictureExtName,SheetPictureExtName:String;
      PersonPictureJPEG,SheetPictureJPEG:TJPEGImage;
      PersonPictureBitMap,SheetPictureBitMap:TBitMap;
    begin
      PersonPictureStream:=TMemoryStream.Create;
      SheetPictureStream:=TMemoryStream.Create;
      if PersonPicturePath<>'' then
      begin
       PersonPictureExtName:=ExtractFileExt(PersonPicturePath); //取得图片文件扩展名
        if (UpperCase(PersonPictureExtName)='.JPG') or (UpperCase(PersonPictureExtName)='.JPEG') then  //转换为大写
        begin
          try
            PersonPictureJPEG:=TJPEGImage.Create;
            PersonPictureJPEG.LoadFromFile(PersonPicturePath);
            PersonPictureJPEG.SaveToStream(PersonPictureStream);
            PersonPictureStream.Position:=0;
          finally
            PersonPictureJPEG.Free;
          end;
        end;
        if (UpperCase(PersonPictureExtName)='.BMP') then  //转换为大写
        begin
          try
            PersonPictureJPEG:=TJPEGImage.Create;
            PersonPictureBitMap:=TBitMap.Create;
            PersonPictureBitMap.LoadFromFile(PersonPicturePath);
            PersonPictureJPEG.Assign(PersonPictureBitMap);
            PersonPictureJPEG.CompressionQuality:=10;
            PersonPictureJPEG.Compress;
            PersonPictureJPEG.SaveToStream(PersonPictureStream);
            PersonPictureStream.Position:=0;
          finally
            PersonPictureBitMap.Free;
            PersonPictureJPEG.Free;
          end;
        end;
        TBlobField(DM.PersonQuery.FieldByName('PersonPicture')).LoadFromStream(PersonPictureStream);
       end;一小段 让楼主见笑了
      

  3.   

    很废硬盘空间,建议不要在Access里使用
      

  4.   

    上面的樓兄已經寫得很不錯了,不過我在這裡提一點我自己的建議:
    可以在保存圖形的時候做一次選擇性壓縮,為什麼說選擇性壓縮呢?因為在圖形文件中,有很多格式都是已經壓縮過的,如JPG,所以對於這類圖形文件可以不壓縮。當讀取的時候,先作一次對應的選擇性解壓縮,然後再顯示即可對於壓縮,我覺得用Delphi自帶的Zlib還可以,當然你也可以用其它第三方專業控件
      

  5.   

    用 blob  存取,网上例子多了,