Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
how can i convert Json to image directly?
I tried to convert json to image directly, but I couldnt. how can I do it?

What I have tried:

Here is the json structure:
{"objects":[{"type":"text","originX":"left","originY":"top","left":15.87,"top":10.24,"width":491.24,"height":56.5,"fill":"#ffffff","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":0.4,"scaleY":0.4,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0,"text":"آغاز بارندگي = خطر لغزندگي","fontSize":"50","fontWeight":"","fontFamily":"B Titr","fontStyle":"normal","lineHeight":1,"underline":false,"overline":false,"linethrough":false,"textAlign":"left","textBackgroundColor":"","charSpacing":0,"styles":{}}],"background":"#000"}

And this is the code to convert:
Bitmap bm = new Bitmap(384, 80);
                       //bm = new <pre>if (!mapNRPRMTORenderJsonFileToImage.ReqListInfo(dt_VMS))
           {
               return false;
           }
           int i = 0;
           App_Code.ImageConverter imgConverter = new App_Code.ImageConverter();

           foreach (DataRow dr_VMS in dt_VMS.Rows)
           {
               try
               {
                   var rootObject = Newtonsoft.Json.JsonConvert.DeserializeObject<App_Code.Rootobject>(dr_VMS["JasonForImageFile"].ToString());
                   if (rootObject.objects[0].type == "text")
                   {
                       //Bitmap image = new Bitmap(rootObject.objects[0].,int.Parse(dr_VMS["MapWidth_1"].ToString()), int.Parse(dr_VMS["MapHeight_1"].ToString()));
                       // = ;
                       //  image.Save(@"D:\Images\" + "image_" + i + ".png");
                       Bitmap bm = new Bitmap(384, 80);
                       //bm = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Bitmap>(dr_VMS["JasonForImageFile"].ToString());
                       string image = dr_VMS["JasonForImageFile"].ToString();

                       byte[] byteBuffer = Convert.FromBase64String(image);
                       MemoryStream memoryStream = new MemoryStream(byteBuffer);
                       memoryStream.Position = 0;

                       bm = (Bitmap)Bitmap.FromStream(memoryStream);
                       // Bitmap bmpImage = GetImageBitmapFromUrl1(firstitem["img_url"]);
                   }
               }
               catch
               { }
           }
           return true;
Posted
Updated 8-Oct-18 3:00am
Comments
Kornfeld Eliyahu Peter 8-Oct-18 4:54am    
How that JSON represents an image exactly? Those are drawing commands and settings?
What do you mean be 'directly'? Have you found an indirect way to do so?

1 solution

(You need to reply to comments or the commenter won't see the alert)
Images are represented in several ways on an upload, usually as a large chunk of hexidecimal (0-9A-F). This string can be parsed into bytes and then into an image. It's a literal representation of the 1's and 0's that make up an image.

You Json has nothing like that. There is no image.
What you have there are, at most, a canvas state. There aren't even any details on what the canvas contains other that what colour / thickness the lines would be.

If you are trying to copy an html canvas then you can upload it as an image. You just need to be a bit clever about it.

One way is:
JavaScript
var canvas = document.getElementById("theCanvas");
var img    = canvas.toDataURL("image/png");


This will encode the canvas image as a base64 url string. You can turn that into an actual image in c#.

Here's some more details:
how to save canvas image to server[^]
 
Share this answer
 
Comments
maysam_p82 9-Oct-18 1:36am    
Thank you for your response. But here my application is a windows service . There is no asp.net web page. Also the json contains an image with black background with a text for traffic signs. I already could retrieve the image from json but the font is too big and the text didnt palce at the middle of the background.
Andy Lanng 9-Oct-18 4:00am    
Ah - Then my response won't help but neither can I :S

Best of luck ^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900