我这里有一个通过文件名取服务器上文件的图标的例程,可以改动一下用来取图片数据.
服务器端代码:
//向客户端返回数据
procedure TXFileSystem.GetExeFileIcon(const AFileName: WideString;
out AIconData: OleVariant);
var
mm: TMemoryStream;
MyBuffer: Pointer;
begin
mm := TMemoryStream.Create;
try
try
Self.ExtractExeFileIcon(AFileName, mm);
mm.Position := 0;
AIconData := VarArrayCreate([0, mm.Size - 1], VarByte);
MyBuffer := VarArrayLock(AIconData);
mm.Position := 0;
mm.ReadBuffer(MyBuffer^, mm.Size);
VarArrayUnlock(AIconData);
except
on e: Exceptiondo
raise Exception.Create('取Exe文件的图标出错: ' + e.Message);
end;
finally
mm.Free;
end;
end;
//服务器端通过文件名取图标,存入内存流
procedure TXFileSystem.ExtractExeFileIcon(AFileName: string;
mm: TMemoryStream);
var
aIcon: TAdvancedIcon;
IconExe: TIconLibrary;
fpath: string;
ico: TIcon;
sm: TMemoryStream;
begin
aIcon := TAdvancedIcon.Create;
IconExe := TIconLibrary.Create;
try
if SameText(ExtractFileExt(AFileName), '.exe') then
begin
Iconexe.LoadFromFile(AFileName);
if IconExe.Icons.Count >= 1 then
Aicon := TAdvancedIcon(Iconexe.Icons.Objects[0])
else
begin
ico := Ticon.Create;
sm := TMemoryStream.Create;
ImageList1.GetIcon(0, ico);
ico.SaveToStream(sm);
sm.Position := 0;
aicon.LoadFromStream(sm);
ico.Free;
sm.Free;
end;
aIcon.SaveToStream(mm);
end;
if SameText(ExtractFileExt(AFileName), '.lnk') then
begin
lnk.FILENAME := AFileName;
if ExtractFileExt(lnk.LinkTarget) <> '' then
fpath := lnk.LinkTarget
else
fpath := lnk.IconLocation;
if (fpath <> '') and (SameText(ExtractFileExt(fpath), '.ico') <> True)
then
begin
try
IconExe.LoadFromFile(fpath);
aIcon :=
TAdvancedIcon(IconExe.Icons.Objects[lnk.IconIndex]);
aIcon.SaveToStream(mm);
except
ico := Ticon.Create;
ImageList1.GetIcon(0, ico);
ico.SaveToStream(mm);
ico.Free;
end;
end else
begin
if SameText(ExtractFileExt(fpath), '.ico') then
mm.LoadFromFile(fpath)
else
begin
ico := Ticon.Create;
ImageList1.GetIcon(0, ico);
ico.SaveToStream(mm);
ico.Free;
end;
end;
end;
finally
aIcon.Free;
IconExe.Free;
end;
end;
//客户端代码
//与服务器相连的接口
function TRmFSysForm.GetFileSysData: IXFileSystemDisp;
begin
if not cnnFileSys.Connected then
begin
cnnFileSys.Host := Self.Host;
cnnFileSys.Open;
end;
Result := IXFileSystemDisp(IDispatch(cnnFileSys.AppServer));
end;
//通过接口取得图标数据
function TRmFSysForm.GetFileIcon: TIcon;
var
ov: OleVariant;
ms: TStream;
p: Pointer;
begin
if (Trim(Self.FileName) = '') then
begin
Result := nil;
Exit;
end;
if SameText(ExtractFileExt(Self.FileName), '.exe') then
GetFileSysData.GetExeFileIcon(Self.FileName, ov)
else
GetFileSysData.GetFileIcon(Self.FileName, ov);
if VarIsEmpty(ov) then
begin
Result := nil;
end else
begin
ms := TMemoryStream.Create;
try
p := VarArrayLock(ov);
ms.Write(p^, VarArrayHighBound(ov, 1));
VarArrayUnlock(ov);
ms.Position := 0;
FFileIcon.LoadFromStream(ms);
Result := FFileIcon;
finally
ms.Free;
end;
end;
end;