如何直接将json转换为图像? [英] How can I convert json to image directly?

查看:966
本文介绍了如何直接将json转换为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何直接将Json转换为图像?

我试图直接将json转换为图像,但我无法做到。我怎么能这样做?



我尝试过:



这是json结构:

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;

推荐答案

(您需要回复评论或评论者不会看到警报)

图像在上传时以多种方式表示,通常作为一大块十六进制(0-9A-F)。该字符串可以解析为字节,然后解析为图像。它是构成图像的1和0的字面表示。



You Json没有这样的东西。没有图像。

你所拥有的最多是一个画布状态。关于画布包含什么,甚至没有任何关于线条的颜色/粗细的细节。



如果你想复制一个html画布然后你可以将其上传为图像。你需要对它有点聪明。



一种方法是:

(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:
var canvas = document.getElementById("theCanvas");
var img    = canvas.toDataURL("image/png");





这会将画布图像编码为base64 url​​字符串。您可以将其转换为c#中的实际图像。



这里有更多详细信息:

如何将画布图像保存到服务器 [ ^ ]


这篇关于如何直接将json转换为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆