I have this code where I send an jpg image with TidTCPClient to a TidTCPServer.
On the Server side the image aperea in a TImage, but no file is created when I save it. I tried using .jpg as well I try to use the MemoryStream.SaveToFile.
I look in many forums and it looks like the do the same thing, what is wrong with this code?
Here is the server code.
procedure TMainForm.IdTCPServer1Execute(AContext: TIdContext);
var
JSON: TJSONObject;
StringStream: TStringStream;
MemoryStream: TMemoryStream;
begin
MemoryStream := nil;
StringStream := nil;
JSON := nil;
try
StringStream := TStringStream.Create('', TEncoding.ASCII);
AContext.Connection.IOHandler.LargeStream := True;
AContext.Connection.IOHandler.ReadStream(StringStream, SizeOf(StringStream), True);
JSON := TJSONObject.ParseJSONValue(StringStream.DataString) as TJSONObject;
MemoryStream := TMemoryStream.Create;
IdDecoderMIME1.DecodeStream(JSON.GetValue('image_encoded').Value, MemoryStream);
TThread.Synchronize(nil,
procedure
begin
Edit2.Text := '';
Edit2.Text := JSON.GetValue('message').Value;
Image1.Bitmap.LoadFromStream(MemoryStream);
end);
finally
MemoryStream.Free;
StringStream.Free;
JSON.Free;
end;
SaveImageToDisk;
end;
procedure TMainForm.SaveImageToDisk;
var
CsFilename: String;
begin
CsFilename := Edit2.Text + '.png';
Image1.Bitmap.SaveToFile(CsFilename);
end;