TcxGrid Column动态添加Image

      MyCol := TcxColumn.Create;
            ...

            MyCol.PropertiesClass := TcxImageProperties;
            ImageProps := TcxImageProperties(MyCol.Properties);
            ImageProps.Center := True;
            ImageProps.GraphicClassName := '';
            ImageProps.OnGetGraphicClass := GetThumbnailGraphicClass;
            ImageProps.Stretch := True;
            ...

Procedure GetThumbnailGraphicClass:

procedure TCORSA.GetThumbnailGraphicClass(AItem: TObject;
  ARecordIndex: Integer; APastingFromClipboard: Boolean;
  var AGraphicClass: TGraphicClass);
begin
  if AnsiSAmeText(FThumbNailExtension, '.TIF') then
      AGraphicClass := TGraphicClass(GetClass('TTiffGraphic'))
  else
  if AnsiSAmeText(FThumbNailExtension, '.JPG') then
      AGraphicClass := TGraphicClass(GetClass('TJPEGImage'))
end;

The actual thumbnail data is loaded into the grid via streams:

                MStream := TMemoryStream.Create;
                Stream := TStringStream.Create('');

                MStream.LoadFromFile(ThumbNail);
                Stream.CopyFrom(MStream, MStream.Size);

                FActiveGrid.DataController.SetValue(RowInfo.RecordIndex,
                                                    ThumbCol,
                                                    Stream.DataString);

改进后的:

 

function StreamToVar(Stream: TStream): OleVariant;
var
      P: Pointer;
begin
  Result := VarArrayCreate([0, Stream.size -1],Varbyte);
  P := VarArrayLock(Result);
  Try
    Stream.Position := 0;
    Stream.Read(P^, Stream.size);
  Finally
    VarArrayUnlock(Result);
  end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
  IRecIdx  :  Integer;
  stream : TMemoryStream;
begin
  with cxGrid1TableView1.DataController do
  begin
    IRecIdx := AppendRecord;
    stream := TMemoryStream.Create();
    stream.LoadFromFile('H:pic随拍IMAG0002.jpg');
     stream.Position := 0;
    Values[IRecIdx,0] := StreamToVar(stream);
    stream.Free;
    Post;
  end;
end;
原文地址:https://www.cnblogs.com/starluck/p/3925863.html