Author: Tomas Rutkauskas
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:
1 unit BmpToFromDB;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7 StdCtrls, FileCtrl, DBCtrls, ExtCtrls, Db, DBTables, Menus;
8
9 type
10 TFrmBmpToFromDB = class(TForm)
11 DriveComboBox1: TDriveComboBox;
12 DirectoryListBox1: TDirectoryListBox;
13 FileListBox1: TFileListBox;
14 BtnWriteS: TButton;
15 Image1: TImage;
16 DataSource1: TDataSource;
17 Table1: TTable;
18 Table1TheLongInt: TIntegerField;
19 Table1ABlobField: TBlobField;
20 Table1Bytes1: TBlobField;
21 Table1Bytes2: TBytesField;
22 Table1B32_1: TBlobField;
23 Table1B32_2: TBytesField;
24 DBNavigator1: TDBNavigator;
25 DBImage1: TDBImage;
26 BtnReadS: TButton;
27 BtnWrite: TButton;
28 BtnRead: TButton;
29 procedure FormCreate(Sender: TObject);
30 procedure FormDestroy(Sender: TObject);
31 procedure BtnWriteSClick(Sender: TObject);
32 procedure BtnReadSClick(Sender: TObject);
33 procedure BtnReadClick(Sender: TObject);
34 procedure BtnWriteClick(Sender: TObject);
35 private
36 { Private declarations }
37 public
38 { Public declarations }
39 end;
40
41 var
42 FrmBmpToFromDB: TFrmBmpToFromDB;
43
44 implementation
45
46 {$R *.DFM}
47
48 procedure TFrmBmpToFromDB.FormCreate(Sender: TObject);
49 begin
50 Table1.Open;
51 end;
52
53 procedure TFrmBmpToFromDB.FormDestroy(Sender: TObject);
54 begin
55 Table1.Close;
56 end;
57
58 procedure TFrmBmpToFromDB.BtnWriteSClick(Sender: TObject);
59 var
60 f: integer;
61 theBitmap: TBitmap;
62 theBlobStream: TBlobStream;
63 begin
64 for f := 0 to FileListBox1.Items.Count - 1 do
65 begin
66 Table1.Edit;
67 theBlobStream := TBlobStream.Create(Table1B32_1, bmReadWrite);
68 try
69 theBitmap := TBitmap.Create;
70 try
71 theBitmap.LoadFromFile(FileListBox1.Items[f]);
72 theBitmap.SaveToStream(theBlobStream);
73 finally
74 theBitmap.Free;
75 end;
76 finally
77 theBlobStream.Free;
78 end;
79 Table1.Post;
80 Table1.Next;
81 end;
82 Table1.First;
83 DBImage1.Datasource := Datasource1;
84 end;
85
86 procedure TFrmBmpToFromDB.BtnWriteClick(Sender: TObject);
87 var
88 f: integer;
89 theBitmap: TBitmap;
90 begin
91 for f := 0 to FileListBox1.Items.Count - 1 do
92 begin
93 Table1.Edit;
94 theBitmap := TBitmap.Create;
95 try
96 TBlobField(Table1.FieldByName('B32_1')).LoadFromFile(FileListBox1.Items[f]);
97 finally
98 theBitmap.Free;
99 end;
100 Table1.Post;
101 Table1.Next;
102 end;
103 Table1.First;
104 DBImage1.Datasource := Datasource1;
105 end;
106
107 procedure TFrmBmpToFromDB.BtnReadSClick(Sender: TObject);
108 var
109 tempBmp: TBitmap;
110 theBlobStream: TBlobStream;
111 begin
112 tempBmp := TBitmap.Create;
113 try
114 theBlobStream := TBlobStream.Create(TBlobField(Table1.FieldByName('B32_1')),
115 bmRead);
116 try
117 tempBmp.LoadFromStream(theBlobStream);
118 Image1.Picture.Bitmap.Assign(tempBmp);
119 finally
120 theBlobStream.Free;
121 end;
122 finally
123 tempBmp.Free;
124 end;
125 end;
126
127 procedure TFrmBmpToFromDB.BtnReadClick(Sender: TObject);
128 var
129 tempBmp: TBitmap;
130 begin
131 tempBmp := TBitmap.Create;
132 try
133 tempBmp.Assign(TBlobField(Table1.FieldByName('B32_1')));
134 Image1.Picture.Bitmap.Assign(tempBmp);
135 finally
136 tempBmp.Free;
137 end;
138 end;
139
140 end.
|