我用Dbimage连接数据库的Image(Access里看到数据类型是Ole对像,在SQL数据库里看到的是数据类型是Image长度为16,在SQL数据库字段里看到的内容是<Binary>)字段请问如何才能读出这里面的数据并显示图形

解决方案 »

  1.   

    DBImage 之所以無法正常顯示圖形,是因為當初該欄位的圖形資料是以 OLE 物件的格式存進去的(跟是不是 Access 資料庫沒關係),所以解決方法就是將 OLE 格式的圖形資料轉換成 DBImage 支援的 bitmap 格式,參考以下步驟:刪除 DBImage1,並且將以下程式碼貼到你的程式裡:procedure LoadImageFromField(APicture: TPicture; AField: TBlobField);
    var
      ABitmap: TBitmap;
      AStream: TMemoryStream;
    begin
      if AField.IsNull then
        Exit;  AStream := TMemoryStream.Create;
      try
        AField.SaveToStream(AStream);    // Skip OLE storage header
        AStream.Seek(78, soFromBeginning);
        ABitmap := TBitmap.Create;
        try
          ABitmap.LoadFromStream(AStream);
          APicture.Graphic := ABitmap;
        finally
          ABitmap.Free;
        end;
      finally
        AStream.Free;
      end;
    end;
       在 ADODataSet1 的 AfterScroll 事件中呼叫 LoadImageFromField 函式:procedure TForm1.ADOTable1AfterScroll(DataSet: TDataSet);
    begin
      LoadImageFromField(
        Image1.Picture,
        DataSet.FieldByName('圖片') as TBlobField
      );
    end; 編譯並執行程式,現在你應該可以看到圖形欄位正確地顯示在 Image1 裡面了。
      

  2.   

    贴点代码:
    添加图片
      q_op2.Close;
      q_op2.SQL.Text:='select * from t_jkdpic where id=''1'';
      q_op2.Open;
      if q_op2.Eof then
         q_op2.Insert
      else
         q_op2.Edit; if opendialog1.Execute then
      begin
        AssignFile(f, opendialog1.FileName);
        Reset(f);
        try
          size := FileSize(f);
          if size > 512000 * 4 then
          begin
            showmessage('此图片的大小不能大于2M');
            exit;
          end;
        finally
          CloseFile(f);
        end;
      try
      q_op2.FieldByName('id').AsString:=trim(q_xxcx.fieldbyname('id').AsString);
      Tblobfield(q_op2.Fieldbyname('pic')).loadfromfile(Op1.FileName);
      q_op2.Post;
      showmessage('图片添加完毕!');
      except
      showmessage('图片添加失败!');
      end;
      end;
    显示图片
      q_op6.Close;
      q_op6.SQL.Text:='select * from t_jkdpic where id=''' + gp_jkd + '''';
      q_op6.Open;
      if not q_op6.Eof then
      begin
      Tblobfield(q_op6.FieldByName('pic')).SaveToFile('C:\tp.jpg');      image1.Picture.LoadFromFile('C:\tp.jpg');
      end;
      

  3.   

    我已经用了另外一种方法
    在数据库中不保存图片,只是保存图片的路径,显示图片时根据图片的路径装载到DBImage中
    这样的话,不至于造成数据表太大,你可以试试
      

  4.   

    //to greending
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Grids, DBGrids, DB, ADODB, ExtCtrls;type
      TForm1 = class(TForm)
        ADOConnection1: TADOConnection;
        ADODataSet1: TADODataSet;
        DataSource1: TDataSource;
        DBGrid1: TDBGrid;
        Image1: TImage;
        procedure ADODataSet1AfterScroll(DataSet: TDataSet);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation
    procedure LoadImageFromField(APicture: TPicture; AField: TBlobField);
    var
      ABitmap: TBitmap;
      AStream: TMemoryStream;
    begin
      if AField.IsNull then
        Exit;  AStream := TMemoryStream.Create;
      try
        AField.SaveToStream(AStream);    // Skip OLE storage header
        AStream.Seek(78, soFromBeginning);
        ABitmap := TBitmap.Create;
        try
          ABitmap.LoadFromStream(AStream);
          APicture.Graphic := ABitmap;
        finally
          ABitmap.Free;
        end;
      finally
        AStream.Free;
      end;
    end;{$R *.dfm}procedure TForm1.ADODataSet1AfterScroll(DataSet: TDataSet);
    begin
      LoadImageFromField(
        Image1.Picture,
        DataSet.FieldByName('SDimage') as TBlobField
      );
    end;
    end.
    //之所以用TADODataSet是由于数据过多
    //TADODataSet.CommandText='select top 5 from Table'
    //Bitmap image is not valid 报错
      

  5.   

    (adoquery1.FieldByName('fieldname1') as TBlobField).SaveToFile('c:\tmp11');
      image1.Picture.Bitmap.LoadFromFile('c:\tmp11');
      deletefile('c:\tmp11');
    也是Bitmap image is not valid这种提示高手救命啊