我的办法是将Graphic的类名放在流的前面,代码如下
procedure SavePictureToBlobField(G: TGraphic; F: TBlobFiled);
var
CName: ShortString;
S: TBlobStream;
begin
if G = nil then Exit;
S := TBlobStream.Create(F, bmWrite);
try
CName := G.ClassName,
S.Write(CName, Length(CName) + 1);
G.SaveToStream(S);
finally
S.Free;
end;
end;
procedure ReadPictureFromBlobField(G: TGraphic; F: TBlobFiled);
var
CName: ShortString;
S: TBlobStream;
begin
if G = nil then Exit;
S := TBlobStream.Create(F, bmRead);
try
S.Read(CName[0], 1);
S.Read(CName[1], Integer(CName[0]));
if SameText(CName, 'TBitmap') then
G := TBitmap.Create
else if SameText(CName, 'TJPEGImage) then
G := TJPEGImage.Create
else if ... //其他的格式
else begin
raise Exception.Create('无效的图片格式!');
end;
G.LoadFromStream(S);
finally
S.Free;
end;
end;