我写了一个TDBJPGImage控件,可作参考。若要完整程序可EMail我。
部分源代码如下:
TDbJPEGImage = class(TImage)
......
end;
procedure TDbJPEGImage.DoDataChange(Sender: TObject);
var Fld: TBlobField;
Strm: TMemoryStream;
Jpeg: TJPEGImage;
begin
if not (FDataLink.Field is TBlobField) then
Exit;
Fld := FDataLink.Field as TBlobField;
if Fld.IsNull then
begin
Picture := nil;
Exit;
end;
Strm := TMemoryStream.Create();
Jpeg := TJPEGImage.Create();
try
Fld.SaveToStream(Strm);
Strm.Position := 0;
Jpeg.LoadFromStream(Strm);
Picture.Assign(Jpeg);
finally
Strm.Free();
Jpeg.Free();
end;
end;
procedure TDbJPEGImage.ApplyToDataSet;
var Fld: TBlobField;
Strm: TMemoryStream;
Jpeg: TJPEGImage;
begin
if not Assigned(FDataLink.DataSet) then
Exit;
if not FDataLink.DataSet.Active then
Exit;
if not (FDataLink.Field is TBlobField) then
Exit;
Fld := FDataLink.Field as TBlobField;
Strm := TMemoryStream.Create();
Jpeg := TJPEGImage.Create();
try
FDataLink.DataSet.Edit();
if (Picture = nil) or
(Picture.Graphic = nil) then
begin
Fld.Clear();
end
else
begin
Jpeg.Assign(Picture.Graphic);
Jpeg.SaveToStream(Strm);
Strm.Position := 0;
Fld.LoadFromStream(Strm);
end;
finally
Strm.Free();
Jpeg.Free();
end;
end;