2010. november 14., vasárnap

How to save a Paradox blob field to a file


Problem/Question/Abstract:

My Paradox Table has a BLOB field which contains BMP files (pasted into it). Now I want to access those BLOB values and save them into files... This should be hidden to the user, so I want to use a loop that accesses each record and retrieves that BLOB value. I have to use FieldByName("Cover") to do this. But then I'm lost between all of the formats of TField, TBlobField, etc.. What is the method to access those bmp BLOBs, and then save the picture part to a file? I can't use a DBImage or something similar as I am not showing them on screen during that operation, so I can't use the "Picture" property to retrieve it. It's directly pure table access. Also I have disabled the controls in the loop, so I can't even use a hidden DBImage to do this.

Answer:

Here's something that should get you started:

unit BmpToFromDB;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, FileCtrl, DBCtrls, ExtCtrls, Db, DBTables, Menus;

type
  TFrmBmpToFromDB = class(TForm)
    DriveComboBox1: TDriveComboBox;
    DirectoryListBox1: TDirectoryListBox;
    FileListBox1: TFileListBox;
    BtnWriteS: TButton;
    Image1: TImage;
    DataSource1: TDataSource;
    Table1: TTable;
    Table1TheLongInt: TIntegerField;
    Table1ABlobField: TBlobField;
    Table1Bytes1: TBlobField;
    Table1Bytes2: TBytesField;
    Table1B32_1: TBlobField;
    Table1B32_2: TBytesField;
    DBNavigator1: TDBNavigator;
    DBImage1: TDBImage;
    BtnReadS: TButton;
    BtnWrite: TButton;
    BtnRead: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure BtnWriteSClick(Sender: TObject);
    procedure BtnReadSClick(Sender: TObject);
    procedure BtnReadClick(Sender: TObject);
    procedure BtnWriteClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmBmpToFromDB: TFrmBmpToFromDB;

implementation

{$R *.DFM}

procedure TFrmBmpToFromDB.FormCreate(Sender: TObject);
begin
  Table1.Open;
end;

procedure TFrmBmpToFromDB.FormDestroy(Sender: TObject);
begin
  Table1.Close;
end;

procedure TFrmBmpToFromDB.BtnWriteSClick(Sender: TObject);
var
  f: integer;
  theBitmap: TBitmap;
  theBlobStream: TBlobStream;
begin
  for f := 0 to FileListBox1.Items.Count - 1 do
  begin
    Table1.Edit;
    theBlobStream := TBlobStream.Create(Table1B32_1, bmReadWrite);
    try
      theBitmap := TBitmap.Create;
      try
        theBitmap.LoadFromFile(FileListBox1.Items[f]);
        theBitmap.SaveToStream(theBlobStream);
      finally
        theBitmap.Free;
      end;
    finally
      theBlobStream.Free;
    end;
    Table1.Post;
    Table1.Next;
  end;
  Table1.First;
  DBImage1.Datasource := Datasource1;
end;

procedure TFrmBmpToFromDB.BtnWriteClick(Sender: TObject);
var
  f: integer;
  theBitmap: TBitmap;
begin
  for f := 0 to FileListBox1.Items.Count - 1 do
  begin
    Table1.Edit;
    theBitmap := TBitmap.Create;
    try
      TBlobField(Table1.FieldByName('B32_1')).LoadFromFile(FileListBox1.Items[f]);
    finally
      theBitmap.Free;
    end;
    Table1.Post;
    Table1.Next;
  end;
  Table1.First;
  DBImage1.Datasource := Datasource1;
end;

procedure TFrmBmpToFromDB.BtnReadSClick(Sender: TObject);
var
  tempBmp: TBitmap;
  theBlobStream: TBlobStream;
begin
  tempBmp := TBitmap.Create;
  try
    theBlobStream := TBlobStream.Create(TBlobField(Table1.FieldByName('B32_1')), bmRead);
    try
      tempBmp.LoadFromStream(theBlobStream);
      Image1.Picture.Bitmap.Assign(tempBmp);
    finally
      theBlobStream.Free;
    end;
  finally
    tempBmp.Free;
  end;
end;

procedure TFrmBmpToFromDB.BtnReadClick(Sender: TObject);
var
  tempBmp: TBitmap;
begin
  tempBmp := TBitmap.Create;
  try
    tempBmp.Assign(TBlobField(Table1.FieldByName('B32_1')));
    Image1.Picture.Bitmap.Assign(tempBmp);
  finally
    tempBmp.Free;
  end;
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése