delphi - How to convert a JSON string to an image? -


i have application converts image json array , i'm saving blob field:

function getimage(): string; var   memorystream : tmemorystream;   jsonarray    : tjsonarray;  begin   memorystream := tmemorystream.create;   myimage.picture.graphic.savetostream(memorystream);   memorystream.position := 0;    jsonarray := tjsonarray.create;   jsonarray := tdbxjsontools.streamtojson(memorystream, 0, memorystream.size);    memorystream.free;    result := jsonarray.tostring; end; 

the result of convertion of png image looks this:

[137,80,78,71,13,10,26,10,0,0, ... ] 

this result stored in blob field code:

stream := tstream.create; stream := cdspesquisa.createblobstream(fieldaux, bmread); stream.position := 0;  jsonarray := tjsonarray.create; jsonarray := tdbxjsontools.streamtojson(stream, 0, stream.size); 

here, have same json string [137,80,78,71,13,10,26,10,0,0, ... ] @ beginning, need convert json array image again.

how can achieve that?

as suggested ken, it's simplest forget json , write directly blob field.

saving

stream := tmemorystream.create; try   myimage.picture.graphic.savetostream(stream);   stream.position := 0;   fieldaux.loadfromstream(stream);   stream.free; end; 

loading

stream := tmemorystream.create; try   fieldaux.savetostream(stream);   stream.position := 0;   myimage.picture.graphic.loadfromstream(stream);   stream.free; end; 

you appear insistent in comments json need. i'm find little hard believe. anyway, convert json array, held in string, image, this:

jsonarray := tjsonobject.parsejsonvalue(jsonstring) tjsonarray; try   stream := tdbxjsontools.jsontostream(jsonarray);   try     stream.position := 0;     myimage.picture.graphic.loadfromstream(stream);       stream.free;   end;   jsonarray.free; end; 

also, best of knowledge, code calls tdbxjsontools.streamtojson not work. yields empty json array object.


finally, code leaking sieve. under mis-apprehension don't need free objects create. mistakenly instantiating objects , overwriting new object different object, leaking first. take @ how created objects, , compare code. example:

jsonarray := tjsonarray.create ; jsonarray := tdbxjsontools.streamtojson( memorystream , 0 , memorystream.size ) ; 

here instantiate tjsonarray object , store reference object in jsonarray. job free reference. abandon reference in next line when overwrite reference new object returned streamtojson. since never free jsonarray @ leak not one, 2 objects.

you need brush basic understanding of delphi lifetime management.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -