1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Jpeg, pngimage, GIFImg,
Dialogs, ADODB, DB, Grids, DBGrids, SMDBGrid, ExtCtrls;
type
TFileExtension = Record
eExtension : String;
eImage : TGraphic;
end;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOTblDocuments: TADOTable;
ADOQry: TADOQuery;
SMDBGrid1: TSMDBGrid;
DataSource1: TDataSource;
procedure FormCreate(Sender: TObject);
procedure SMDBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
private
{ Déclarations privées }
procedure LectureExtension;
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
FileExtension : Array of TFileExtension;
implementation
{$R *.dfm}
procedure TForm1.LectureExtension;
var
n : Integer;
BS: TStream;
Graphic: TGraphic;
FirstBytes: AnsiString;
begin
Graphic := nil;
SetLength(FileExtension,0);
Finalize(FileExtension);
With ADOQry do
begin
Sql.Clear;
Sql.Add('Select * From Extension');
try
Active := True;
SetLength(FileExtension,Recordset.RecordCount);
for n := 0 to High(FileExtension) do
begin
FileExtension[n].eExtension := FieldByName('Extension').AsString;
BS := CreateBlobStream(FieldByName('Image'), bmRead);
try
SetLength(FirstBytes, 8);
BS.Read(FirstBytes[1], 8);
if Copy(FirstBytes, 1, 2) = 'BM' then
begin
Graphic := TBitmap.Create;
end else
if FirstBytes = #137'PNG'#13#10#26#10 then
begin
Graphic := TPngImage.Create;
end else
if Copy(FirstBytes, 1, 3) = 'GIF' then
begin
Graphic := TGIFImage.Create;
end else
if Copy(FirstBytes, 1, 2) = #$FF#$D8 then
begin
Graphic := TJPEGImage.Create;
end;
if Assigned(Graphic) then
begin
try
BS.Seek(0, soFromBeginning);
Graphic.LoadFromStream(BS);
FileExtension[n].eImage.Assign(Graphic);
except
end;
Graphic.Free;
end;
finally
BS.Free;
end;
next;
end;
finally
Close;
end;
end;
end;
procedure TForm1.SMDBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
Var
xRect: TRect;
i: Integer;
begin
With Sender as TSMDBGrid Do With Canvas Do
Begin
if DataSource.DataSet.RecordCount > 0 then
begin
xRect := Rect;
FillRect(xRect);
if Column.FieldName = 'eExtension' then
begin
for I := Low(FileExtension) to High(FileExtension) do
begin
if FileExtension[i].eExtension = DataSource.DataSet.FieldByName('eExtension').AsString then
begin
SMDBGrid1.Canvas.FillRect(xRect);
SMDBGrid1.Canvas.StretchDraw(xRect, FileExtension[0].eImage);
end;
end;
end;
DefaultDrawColumnCell(xRect, DataCol, Column, State);
end;
End;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
LectureExtension;
end;
end. |
Partager