Title: TBitmap to JSON Post by: HalcyonLogic 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 Title: Re: TBitmap to JSON Post by: D.Tkalcec (RTC) 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; Best Regards, Danijel Tkalcec Title: Re: TBitmap to JSON Post by: D.Tkalcec (RTC) 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 Title: Re: TBitmap to JSON Post by: HalcyonLogic 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 |