'Byte to Varbinary(max) to Base64

Ok so i generate an img in c# and save it as follows:

... 
using (MemoryStream ms = new MemoryStream())
{                       
   bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
   byte[] byteImage = ms.ToArray();
   img = Convert.ToBase64String(byteImage);
}

return img

Here i get correct base64 string which I will not write since it is way to long.I return it and send it to SQL Server where I do this :

INSERT INTO mytable(a varbinary(max) column) 
Values(CONVERT(VARBINARY, @theImg))

I send it and convert it like this since the base64string is 18k characters long so it is not possible to write it in varchar(max) in SQL Server.

The result in my table looks something like this :

0x618123112277304B47676414E123684551237414142715141

Ok so now I want to read this and convert it back to base64string in Delphi.

I read the column value from my SQL Server table, and convert it to a stream and then I try to convert it to base64 :

function Encode(): AnsiString;
var
  stream: TMemoryStream;
  Field: TBlobField;
begin
  Field := DataSet.FieldByName('theImg') as TBlobField;
  stream := TMemoryStream.Create;
  try
    Field.SaveToStream(stream);
    result := EncodeBase64(stream.Memory, stream.Size);
  finally
    stream.Free;
  end;
end;

The returned data looks something like this:

aVZCT1J3MEtHZ29BQUFBTlNjaKs0FBQnFRQUFB

But the returned data is way too short and its definitely wrong. Does anyone have any clue as how can I solve this ?

UPDATE:

i now skipped converting to base64 in c# and send it as byte[]. the result in type varbinary inside my table now looks like this:

0x53797374656D2E427974655B5D

Besides the current decoding i do inside delphi i also tried with System.NetEncoding :

function StreamToBase64(): String;
var
  Output: TStringStream;
  Encoding: TBase64Encoding;
  mStream: TMemoryStream;
  Field: TBlobField;
begin
  Field := DataSet.FieldByName('theImg') as TBlobField;
  mStream := TBytesStream.Create;
  try
    Field.SaveToStream(mStream);
    mStream.Position := 0;
    Output := TStringStream.Create('', TEncoding.ASCII);
    try
      Encoding := TBase64Encoding.Create(0);
      try
        Encoding.Encode(mStream, Output);
        Result := Output.DataString;
      finally
        Encoding.Free;
      end;
    finally
      Output.Free;
    end;
  finally
  end;
end;

Converting it still returns back wrong value though.

EDIT

The only wrong thing here was this

INSERT INTO mytable(a varbinary(max) column) 
Values(CONVERT(VARBINARY, @theImg))

i just changed the Values(CONVERT(VARBINARY, @theImg)) into Values(CONVERT(VARBINARY(max), @theImg))

And i get correct result

Ty to @RemyLebeau and our little argument :D

What the issue is now is that the max amount of varchar i get back is only like a third of the img when then gets out as :

enter image description here

and yes i did give varchar(max)

My question now is how do i convert varbinary into varchar in delphi ? i do the correct base64 conversion but since my data already is in base64 but converted to varbinary it returns wrong result.

I tryed the code from this post: Converting TMemoryStream to 'String' in Delphi 2009

and i used answer from Nick Hodges.

But what i am worried about now is does delphi have same limitations as sql ? Like will some imgs not gona be displayed fully like the img above ? or can delphi take in longer strings ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source