为什么上传的照片比保存的小很多 - Delphi 10.3.2, firemonkey
Why uploaded photo is much smaller than saved - Delphi 10.3.2, firemonkey
我有 Delphi 10.3.2
我不明白这种情况:
1)
正在上传约1M的照片
image1.Bitmap.LoadFromFile('test.jpg');
然后我保存相同的照片
image1.Bitmap.SaveToFile('test_new.jpg');
和test_new.jpg大约是3M。为什么???
2)
我想使用 IdHTTP 和 POST 请求从 TImage (test1.jpg - 1MB) 对象向服务器发送照片。
我使用函数 Base64_Encoding_stream 来编码图像。
编码函数后的图像大小(字符串)为 20 MB! ?为什么原始文件只有 1MB?
function Base64_Encoding_stream(_image:Timage): string;
var
base64: TIdEncoderMIME;
output: string;
stream_image : TStream;
begin
try
begin
base64 := TIdEncoderMIME.Create(nil);
stream_image := TMemoryStream.Create;
_image.Bitmap.SaveToStream(stream_image);
stream_image.Position := 0;
output := TIdEncoderMIME.EncodeStream(stream_image);
stream_image.Free;
base64.Free;
if not(output = '') then
begin
Result := output;
end
else
begin
Result := 'Error';
end;
end;
except
begin
Result := 'Error'
end;
end;
end;
....
img_encoded := Base64_Encoding_stream(Image1);
.....
procedure Send(_json:String );
var
lHTTP : TIdHTTP;
PostData : TStringList;
begin
PostData := TStringList.Create;
lHTTP := TIdHTTP.Create(nil);
try
PostData.Add('dane=' + _json );
lHTTP.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0';
lHTTP.Request.Connection := 'keep-alive';
lHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
lHTTP.Request.Charset := 'utf-8';
lHTTP.Request.Method := 'POST';
_dane := lHTTP.Post('http://......./add_photo.php',PostData);
finally
lHTTP.Free;
PostData.Free;
end;
要post 使用 base64 的原始文件,您基本上可以使用自己的代码。您只需要像这样更改 base64 编码例程中使用的流:
function Base64_Encoding_stream(const filename: string): string;
var
stream_image : TStream;
begin
try
// create read-only stream to access the file data
stream_image := TFileStream.Create(filename, fmOpenRead or fmShareDenyWrite);
// the stream position will be ‘0’, so no need to set that
Try
Result := TIdEncoderMIME.EncodeStream(stream_image);
Finally
stream_image.Free;
End;
if length(result) = 0 then
begin
Result := 'Error';
end;
except
Result := 'Error'
end;
end;
此外,我用一些 try/finally 部分重构了您的代码,以确保在发生错误时不会发生内存泄漏。我删除了 try/except 中的 begin/end,因为不需要。
还删除了本地字符串变量以避免双重字符串分配和不必要的 TIdEncoderMIME
base64 对象构造。
我有 Delphi 10.3.2 我不明白这种情况:
1) 正在上传约1M的照片
image1.Bitmap.LoadFromFile('test.jpg');
然后我保存相同的照片
image1.Bitmap.SaveToFile('test_new.jpg');
和test_new.jpg大约是3M。为什么???
2)
我想使用 IdHTTP 和 POST 请求从 TImage (test1.jpg - 1MB) 对象向服务器发送照片。 我使用函数 Base64_Encoding_stream 来编码图像。 编码函数后的图像大小(字符串)为 20 MB! ?为什么原始文件只有 1MB?
function Base64_Encoding_stream(_image:Timage): string;
var
base64: TIdEncoderMIME;
output: string;
stream_image : TStream;
begin
try
begin
base64 := TIdEncoderMIME.Create(nil);
stream_image := TMemoryStream.Create;
_image.Bitmap.SaveToStream(stream_image);
stream_image.Position := 0;
output := TIdEncoderMIME.EncodeStream(stream_image);
stream_image.Free;
base64.Free;
if not(output = '') then
begin
Result := output;
end
else
begin
Result := 'Error';
end;
end;
except
begin
Result := 'Error'
end;
end;
end;
....
img_encoded := Base64_Encoding_stream(Image1);
.....
procedure Send(_json:String );
var
lHTTP : TIdHTTP;
PostData : TStringList;
begin
PostData := TStringList.Create;
lHTTP := TIdHTTP.Create(nil);
try
PostData.Add('dane=' + _json );
lHTTP.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0';
lHTTP.Request.Connection := 'keep-alive';
lHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
lHTTP.Request.Charset := 'utf-8';
lHTTP.Request.Method := 'POST';
_dane := lHTTP.Post('http://......./add_photo.php',PostData);
finally
lHTTP.Free;
PostData.Free;
end;
要post 使用 base64 的原始文件,您基本上可以使用自己的代码。您只需要像这样更改 base64 编码例程中使用的流:
function Base64_Encoding_stream(const filename: string): string;
var
stream_image : TStream;
begin
try
// create read-only stream to access the file data
stream_image := TFileStream.Create(filename, fmOpenRead or fmShareDenyWrite);
// the stream position will be ‘0’, so no need to set that
Try
Result := TIdEncoderMIME.EncodeStream(stream_image);
Finally
stream_image.Free;
End;
if length(result) = 0 then
begin
Result := 'Error';
end;
except
Result := 'Error'
end;
end;
此外,我用一些 try/finally 部分重构了您的代码,以确保在发生错误时不会发生内存泄漏。我删除了 try/except 中的 begin/end,因为不需要。
还删除了本地字符串变量以避免双重字符串分配和不必要的 TIdEncoderMIME
base64 对象构造。