0

Been struggling with this one for a while. We have a old table (SQL Server) that has image type data. I want to get the text.

So far this is what I've done.

  • Try to convert or cast it (not allowed)
  • Try to bring over the data into delphi by calling a SP (I got to the point of having the data assigned to a variant)
  • Looked into a RTF to text function (found something here on SO, if i can just get the image data into a string).

This is the code I have so far. It's attached to a button click for now (will be running in a service). I think the assignment to the report var is not right, and SetString may not be right either. I'm not even sure if i am going about this the right way.

var
report: array of byte;
s: string;

begin
  ADOStoredProc1.Parameters.ParamByName('@EncounterID').Value := '7';

  ADOStoredProc1.Open;
  while not ADOStoredProc1.EOF do
  begin
    report := ADOStoredProc1.FieldByName('Report').Value;
    SetString(s, PAnsiChar(@report[0]), length(report));
    Memo1.Lines.Add(s);

    ADOStoredProc1.Next;
  end;
Johan
  • 74,508
  • 24
  • 191
  • 319
Rob
  • 2,363
  • 7
  • 36
  • 54

1 Answers1

1

I'm a little confused by "RTF image". Do you mean RTF text? Or an image (picture)? From the code, I'm suspecting the former...

I'm not sure why you're using an array of byte and then immediately putting that into a string.

This should work just as well (actually, better, because it doesn't use Value (which is a Variant conversion) and avoids the SetString function call):

while not ADOStoredProc.Eof do
begin
  Memo1.Lines.Add(ADOStoredProc1.FieldByName('Report').AsString;
  ADOStoredProc1.Next;
end;

You'll probably get the RTF formatting in the memo this way, though. If you're trying to strip the formatting, you'll need to use a TRichEdit instead, and use the EM_STREAMIN message to add the content, and then use the TRichEdit.PlainText property. There's an example of doing that here.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • The data is stored as image type, which is binary. It's actually a RTF data file. I seem to recall trying AsString and not having much success with it. I'll look again. Thanks! – Rob Mar 14 '11 at 05:46
  • What type of field does Delphi create for it? (Check ADOStoredProc1.FieldByName('Report').ClassName) If it creates a TBlobField, you can grab the BlobStream and directly add it to the TRichEdit using LoadFromStream; there's code for that in the same Delphi news link I provided in my answer. – Ken White Mar 14 '11 at 06:04
  • Yes, this works to some extent! Still have one remaining issue which I'll ask separately. Thanks. I'll post full code once i get that resolved. Issue is that somehow MSSQL does something to do the binary data when inserting it into the DB. Need to figure out how to translate it back. – Rob Mar 14 '11 at 13:41