RTC Forums
November 24, 2024, 05:58:26 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: TBitmap to JSON  (Read 5726 times)
HalcyonLogic
Newbie
*
Posts: 45


« on: June 18, 2013, 08:48:44 PM »

Can't seem to be able to wrap my head around it.

How would one send a delphi TBitmap binary data into a JSON object/element?

i.e. { "Image": "ABCD1234..." }

And yes, I did take a look at the DelphiDatasetToRtc (which for some reason always returns "RtcDataset").

Code snippet would be very much appreciated.

Of course, please and thank you are in order.

Richard
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: June 18, 2013, 09:11:43 PM »

JSON is not a very suitable format for sending bitmaps and there is no default way for converting binary content into JSON, but you could use the TBitmap.SaveToStream() method to save the content of the TBitmap into a TStream, which will be mime-encoded when you use the "toJSON" method. Ofcourse, you will need to reverse the process on the other side to get the Bitmap back.
 
Code:
uses rtcInfo;

function BitmapToJSONString(bmp:TBitmap):RtcString;
var
  myRecord:TRtcRecord;
  myStream:TStream;
begin
myRecord := TRtcRecord.Create;
try
  myStream := myRecord.newByteStream('Image');
  bmp.SaveToStream( myStream );
  Result := myRecord.toJSON;
finally
  myRecord.Free;
  end;
end;


Best Regards,
Danijel Tkalcec
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #2 on: June 18, 2013, 09:23:00 PM »

By the way ... the above example, slightly modified, would work for anything that has a "SaveToStream" method (or some other way of saving itself to a TStream).

On a side-note: Sending uncompressed bitmaps with JSON is very bandwith-consuming. If you need to send images, I would recommend you to use a better image format than BMP, and stream the image in raw binary format and not encoded into a JSON string. As a comparison, I've taken a Screenshot of my current Desktop and saved it with the above code into JSON and the file was over 10 MB in size. Then, I've taken the same screenshot and stored it as a JPG with standard quality setting in "Paint.exe" and the same image was less than 800 KB (8% of the JSON size).

Best Regards,
Danijel Tkalcec
Logged
HalcyonLogic
Newbie
*
Posts: 45


« Reply #3 on: June 18, 2013, 09:53:45 PM »

Thank you very much Danijel.

Given your recommendations, I will rethink my approach.

In this particular case, I have a handful of images (10 ish) used for a very specific reason. These images will always be the same and will never changed. So I might just create a physical image on the drive/server and return its URL (instead of sending back long binaries).

(I do appreciate you taking the time to supply the sample code though)

Thanks again,
Richard
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.024 seconds with 16 queries.