[英]How to send and receive a TMemoryStream (in a TObject) using DataSnap in Delphi 10.1
我有一個使用 REST Datasnap 在 Delphi 10.1 中編寫的應用程序。
此應用程序包括客戶端和服務器。
客戶端是移動應用程序 (Android),服務器是連接到 firebird 數據庫的 Windows 服務。
我有一個 Object:-
TJob = class(TObject)
private
FID: Integer;
FThe_Name: String;
FImage: TMemoryStream;
public
constructor Create;
destructor Destroy;
end;
constructor TJob.Create;
begin
inherited;
FImage := TMemoryStream.Create;
end;
destructor TJob.Destroy;
begin
FreeAndNil(FImage);
inherited;
end;
當我嘗試將服務器上的圖像保存到數據庫以及嘗試打開並在客戶端上顯示圖像時,我遇到了訪問沖突。
我有一個獨立的多設備應用程序,它使用與客戶端/服務器相同的功能(獲取、保存和顯示圖像)並且可以工作。
客戶在表格上顯示圖像:-
if (Job.Image.Size > 0) then
begin
rectangle.Fill.Kind := TBrushKind.Bitmap;
rectangle.Fill.Bitmap.Bitmap.LoadFromStream(Job.Image);
rectangle.Repaint;
Layout.Repaint;
end;
客戶從表單中獲取圖像:-
if not(rectangle.Fill.Bitmap.Bitmap.IsEmpty) then
begin
Job.Image.Seek(0, soFromBeginning);
rectangle.Fill.Bitmap.Bitmap.SaveToStream(Job.Image);
Job.Image.Position := 0;
end;
服務器將圖像保存到數據庫:-
Job.Image.Position := 0;
(TBlobField(FieldByName('MyImage'))).SaveToStream(Job.Image);
服務器從數據庫獲取圖像:-
(TBlobField(FieldByName('MyImage'))).SaveToStream(Job.Image);
獨立應用程序使用相同的方式工作,但是在嘗試保存或顯示圖像時出現錯誤。
我已經用各種格式的圖像填充了數據庫,我可以在數據庫中查看這些圖像,但不能從客戶端(AV)中查看。
關於我做錯了什么的任何想法以及如何解決修復的示例?
謝謝
服務器從數據庫獲取圖像:-
PngImage := TPngImage.Create;
MemoryStream := TMemoryStream.Create;
try
(TBlobField(FieldByName('Image'))).SaveToStream(MemoryStream);
MemoryStream.Position := 0;
PngImage.LoadFromStream(MemoryStream);
Job.Image_AsStr := Base64FromPngImage(PngImage);
finally
MemoryStream.Free;
end;
客戶在表格上顯示圖像:-
if (Job.Image_AsStr <> '') then
begin
rImage.Fill.Kind := TBrushKind.Bitmap;
rImage.Fill.Bitmap.Bitmap := BitmapFromBase64(Job.Image_AsStr);
rCustomer_Signature.Repaint;
lCustomer_Signature.Repaint;
end;
客戶從表單中獲取圖像:-
if not(rImage.Fill.Bitmap.Bitmap.IsEmpty) then
begin
rImage.Fill.Kind := TBrushKind.Bitmap;
Job.Image_AsStr := Base64FromBitmap(rImage.Fill.Bitmap.Bitmap);
end
else
Job.Image_AsStr := '';
服務器將圖像保存到數據庫:-
if Job.Image_AsStr <> '' then
begin
MemoryStream := TMemoryStream.Create;
try
PngImage := PngImageFromBase64(Job.Image_AsStr);
PngImage.SaveToStream(MemoryStream);
MemoryStream.Position := 0;
Params[1].LoadFromStream(MemoryStream, ftBlob);
finally
MemoryStream.Free;
end;
end
else
Params[1].Clear;
根據我的經驗,圖像作為 PngImage 回到服務器,即使它被打包為 BMP。
我現在可以確認我已經在 Android 手機上進行了安裝和測試。 *筆記。 發布的原始問題是關於使用 TMemoryStream,這些示例使用字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.