Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. not able to Deserialize json signature lines

not able to Deserialize json signature lines

Scheduled Pinned Locked Moved .NET (Core and Framework)
graphicsjson
16 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V ven753

    Thank you very much for replying. How you said like that i got one object with a single property called lines. Now i need to implement by taking into forloop but i am not able to. below is the code. foreach (var line in signature) { signatureGraphic.DrawLine(pen, line.lx, line.ly, line.mx, line.my); } to get into for loop we have to add below code or not please specify private class SignatureLine { public int lx { get; set; } public int ly { get; set; } public int mx { get; set; } public int my { get; set; } } after this it will return return signatureImage; again it will come to default page as shown in below code as a bitmap var signatureImage = sigToImg.SigJsonToImage(json); then this signatureImage is system.drawing.bitmap and i need to save as image as follows Bitmap bmp = new Bitmap(signatureImage); bmp.Save(Server.MapPath("images\\s1.jpg"), ImageFormat.Jpeg); bmp.Dispose(); but i am not able to get into for loop. How to achieve this. Please reply me. Thanks in advance

    Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #4

    Nope, that still doesn't make any sense to me. Your list contains pairs of floating point numbers; how do you intend to get four integer values from that? For example, the first value in your list is: [156.44, 96.44] Given those two numbers, what values are you expecting in the lx, ly, mx and my properties of the SignatureLine class?


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    V 1 Reply Last reply
    0
    • Richard DeemingR Richard Deeming

      Nope, that still doesn't make any sense to me. Your list contains pairs of floating point numbers; how do you intend to get four integer values from that? For example, the first value in your list is: [156.44, 96.44] Given those two numbers, what values are you expecting in the lx, ly, mx and my properties of the SignatureLine class?


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      V Offline
      V Offline
      ven753
      wrote on last edited by
      #5

      I dont know i got code while searching, i think we are at end stage and can we change so that it has to work by changing signatureGraphic.DrawLine parameter signatureGraphic.SmoothingMode = SmoothingMode.AntiAlias; foreach (var lines in signature) { signatureGraphic.DrawLine(pen, lines.lx, line.ly, line.mx, line.my); } so that signatureGraphic can draw line. Please reply. thanks in advance

      Richard DeemingR 1 Reply Last reply
      0
      • V ven753

        I dont know i got code while searching, i think we are at end stage and can we change so that it has to work by changing signatureGraphic.DrawLine parameter signatureGraphic.SmoothingMode = SmoothingMode.AntiAlias; foreach (var lines in signature) { signatureGraphic.DrawLine(pen, lines.lx, line.ly, line.mx, line.my); } so that signatureGraphic can draw line. Please reply. thanks in advance

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #6

        You still haven't explained how you are expecting to get four integers out of each pair of floating-point numbers. Without that information, it is impossible to convert your source data to a series of calls to the DrawLine method. If you don't know what the data means, then you can't do anything with it!


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        V 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          You still haven't explained how you are expecting to get four integers out of each pair of floating-point numbers. Without that information, it is impossible to convert your source data to a series of calls to the DrawLine method. If you don't know what the data means, then you can't do anything with it!


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          V Offline
          V Offline
          ven753
          wrote on last edited by
          #7

          actually that is (lx, ly) is the start point of the line, and (mx, my) is the end point of the line. i think that it is clear. but i dont know how to achieve this. Reply me thanks in advance

          Richard DeemingR 1 Reply Last reply
          0
          • V ven753

            actually that is (lx, ly) is the start point of the line, and (mx, my) is the end point of the line. i think that it is clear. but i dont know how to achieve this. Reply me thanks in advance

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #8

            OK, that makes more sense. There's still the slight issue that the JSON is using floating-point numbers but the DrawLine method expects integers, but you can probably ignore that for now. You'll still need to deserialize to the class I posted previously, but you can use an iterator method to construct the sequence of SignatureLine objects from the data:

            private sealed class SignatureLine
            {
            public int lx { get; set; }
            public int ly { get; set; }
            public int mx { get; set; }
            public int my { get; set; }
            }

            private class Signature
            {
            public List<List<List<double>>> lines { get; set; }

            public IEnumerable<SignatureLine> SignatureLines
            {
                get
                {
                    foreach (List<List<double>> stroke in lines)
                    {
                        if (stroke.Count == 1) continue;
                        
                        List<double> lastPoint = stroke\[0\];
                        Debug.Assert(lastPoint.Count == 2);
                        
                        foreach (List<double> point in stroke.Skip(1))
                        {
                            Debug.Assert(point.Count == 2);
                            
                            // For now, just round the floating point values to the nearest integer:
                            yield return new SignatureLine
                            {
                                lx = (int)Math.Round(lastPoint\[0\]),
                                ly = (int)Math.Round(lastPoint\[1\]),
                                mx = (int)Math.Round(point\[0\]),
                                my = (int)Math.Round(point\[1\])
                            };
                            
                            lastPoint = point;
                        }
                    }
                }
            }
            

            }

            ...

            var serializer = new JavaScriptSerializer();
            var signature = serializer.Deserialize<Signature>(json);
            foreach (SignatureLine line in signature.SignatureLines)
            {
            signatureGraphic.DrawLine(pen, line.lx, line.ly, line.mx, line.my);
            }


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            V 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              OK, that makes more sense. There's still the slight issue that the JSON is using floating-point numbers but the DrawLine method expects integers, but you can probably ignore that for now. You'll still need to deserialize to the class I posted previously, but you can use an iterator method to construct the sequence of SignatureLine objects from the data:

              private sealed class SignatureLine
              {
              public int lx { get; set; }
              public int ly { get; set; }
              public int mx { get; set; }
              public int my { get; set; }
              }

              private class Signature
              {
              public List<List<List<double>>> lines { get; set; }

              public IEnumerable<SignatureLine> SignatureLines
              {
                  get
                  {
                      foreach (List<List<double>> stroke in lines)
                      {
                          if (stroke.Count == 1) continue;
                          
                          List<double> lastPoint = stroke\[0\];
                          Debug.Assert(lastPoint.Count == 2);
                          
                          foreach (List<double> point in stroke.Skip(1))
                          {
                              Debug.Assert(point.Count == 2);
                              
                              // For now, just round the floating point values to the nearest integer:
                              yield return new SignatureLine
                              {
                                  lx = (int)Math.Round(lastPoint\[0\]),
                                  ly = (int)Math.Round(lastPoint\[1\]),
                                  mx = (int)Math.Round(point\[0\]),
                                  my = (int)Math.Round(point\[1\])
                              };
                              
                              lastPoint = point;
                          }
                      }
                  }
              }
              

              }

              ...

              var serializer = new JavaScriptSerializer();
              var signature = serializer.Deserialize<Signature>(json);
              foreach (SignatureLine line in signature.SignatureLines)
              {
              signatureGraphic.DrawLine(pen, line.lx, line.ly, line.mx, line.my);
              }


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              V Offline
              V Offline
              ven753
              wrote on last edited by
              #9

              Now it is looping inside and also values are getting public Bitmap SigJsonToImage(string json) { var signatureImage = GetBlankCanvas(); if (!string.IsNullOrEmpty(json)) { using (var signatureGraphic = Graphics.FromImage(signatureImage)) { signatureGraphic.SmoothingMode = SmoothingMode.AntiAlias; var pen = new Pen(PenColor, PenWidth); var serializer = new JavaScriptSerializer(); var signature = serializer.Deserialize(json); foreach (SignatureLine line in signature.SignatureLines) { signatureGraphic.DrawLine(pen, line.lx, line.ly, line.mx, line.my); } } } return signatureImage; } private Bitmap GetBlankCanvas() { var blankImage = new Bitmap(CanvasWidth, CanvasHeight); blankImage.MakeTransparent(); using (var signatureGraphic = Graphics.FromImage(blankImage)) { signatureGraphic.Clear(BackgroundColor); } return blankImage; } private sealed class SignatureLine { public int lx { get; set; } public int ly { get; set; } public int mx { get; set; } public int my { get; set; } } private class Signature { public List>> lines { get; set; } public IEnumerable SignatureLines { get { foreach (List> stroke in lines) { if (stroke.Count == 1) continue; List lastPoint = stroke[0]; Debug.Assert(lastPoint.Count == 2); foreach (List point in stroke.Skip(1)) { Debug.Assert(point.Count == 2); // For now, just round the floating point values to the nearest integer: yield return new SignatureLine { lx = (int)Math.Round(lastPoint[0]), ly = (int)Math.Round(lastPoint[1]), mx = (int)Math.Round(point[0]), my = (int)Math.Round(point[1]) }; lastPoint = point; } } } }

              Richard DeemingR 1 Reply Last reply
              0
              • V ven753

                Now it is looping inside and also values are getting public Bitmap SigJsonToImage(string json) { var signatureImage = GetBlankCanvas(); if (!string.IsNullOrEmpty(json)) { using (var signatureGraphic = Graphics.FromImage(signatureImage)) { signatureGraphic.SmoothingMode = SmoothingMode.AntiAlias; var pen = new Pen(PenColor, PenWidth); var serializer = new JavaScriptSerializer(); var signature = serializer.Deserialize(json); foreach (SignatureLine line in signature.SignatureLines) { signatureGraphic.DrawLine(pen, line.lx, line.ly, line.mx, line.my); } } } return signatureImage; } private Bitmap GetBlankCanvas() { var blankImage = new Bitmap(CanvasWidth, CanvasHeight); blankImage.MakeTransparent(); using (var signatureGraphic = Graphics.FromImage(blankImage)) { signatureGraphic.Clear(BackgroundColor); } return blankImage; } private sealed class SignatureLine { public int lx { get; set; } public int ly { get; set; } public int mx { get; set; } public int my { get; set; } } private class Signature { public List>> lines { get; set; } public IEnumerable SignatureLines { get { foreach (List> stroke in lines) { if (stroke.Count == 1) continue; List lastPoint = stroke[0]; Debug.Assert(lastPoint.Count == 2); foreach (List point in stroke.Skip(1)) { Debug.Assert(point.Count == 2); // For now, just round the floating point values to the nearest integer: yield return new SignatureLine { lx = (int)Math.Round(lastPoint[0]), ly = (int)Math.Round(lastPoint[1]), mx = (int)Math.Round(point[0]), my = (int)Math.Round(point[1]) }; lastPoint = point; } } } }

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #10

                Since the signatureImage is already a Bitmap, I'm not sure why you're creating a new Bitmap to save it; you should be able to use:

                signatureImage.Save(Server.MapPath("~/images/s1.jpg"), ImageFormat.Jpeg);

                Other than that, there's no obvious problem with the code that's saving the image. You'll need to step through your code to check that you're getting the values out of the JSON, that the pen you're using has a suitable colour and width, and that the lines you're drawing are within the bounds of the picture. The last point is particularly important; if all the lines you're drawing are outside the bounds of the picture, then you won't be able to see them. I've just run your code to a 200 x 200 pixel canvas, using a 1 pixel black pen on a white background, and I get an image with a hand-drawn "S" on it.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                V 1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  Since the signatureImage is already a Bitmap, I'm not sure why you're creating a new Bitmap to save it; you should be able to use:

                  signatureImage.Save(Server.MapPath("~/images/s1.jpg"), ImageFormat.Jpeg);

                  Other than that, there's no obvious problem with the code that's saving the image. You'll need to step through your code to check that you're getting the values out of the JSON, that the pen you're using has a suitable colour and width, and that the lines you're drawing are within the bounds of the picture. The last point is particularly important; if all the lines you're drawing are outside the bounds of the picture, then you won't be able to see them. I've just run your code to a 200 x 200 pixel canvas, using a 1 pixel black pen on a white background, and I get an image with a hand-drawn "S" on it.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  V Offline
                  V Offline
                  ven753
                  wrote on last edited by
                  #11

                  thank you very much, like you have said, i changed picel to width 400 and height 200 and pen size 4 and now am able to get the image. I really liked your response. can i have ur email id if u dont mind so that next time if i get any other work doubt i can send a amail. thanks once again.

                  Richard DeemingR 1 Reply Last reply
                  0
                  • V ven753

                    thank you very much, like you have said, i changed picel to width 400 and height 200 and pen size 4 and now am able to get the image. I really liked your response. can i have ur email id if u dont mind so that next time if i get any other work doubt i can send a amail. thanks once again.

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #12

                    Glad to hear you got it sorted. If you have any future questions, please post them to the forums. That way, more people can see and potentially answer the questions.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    V 2 Replies Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      Glad to hear you got it sorted. If you have any future questions, please post them to the forums. That way, more people can see and potentially answer the questions.


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      V Offline
                      V Offline
                      ven753
                      wrote on last edited by
                      #13

                      Ok. Thank you very much once again.

                      1 Reply Last reply
                      0
                      • Richard DeemingR Richard Deeming

                        Glad to hear you got it sorted. If you have any future questions, please post them to the forums. That way, more people can see and potentially answer the questions.


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        V Offline
                        V Offline
                        ven753
                        wrote on last edited by
                        #14

                        Hi now i am trying to pass that image url from default page to html page as follows public string path = "images/s1.jpg"; and in html ![](Default.aspx?Code=<%= path%>) while loading only image has to display but i am not getting, blank image i am getting. even if i add source directly in html like below if i see in design view image will show and if i send that html file mail as shown below System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(); mail.From = new MailAddress("emailid"); mail.To.Add("emailid"); mail.IsBodyHtml = true; mail.Subject = "Policy Acceptance"; string FilePath = Server.MapPath("~/sendmail.htm"); FileStream f1 = new FileStream(FilePath, FileMode.Open); StreamReader sr = new StreamReader(f1); string str = sr.ReadToEnd(); str = str.Replace("[[[user]]]", "name"); f1.Close(); mail.Body = str; SmtpClient smtp = new SmtpClient("server ip", portnumber); smtp.Send(mail); after sending mail, mail will recieve but image will like blank. I need to pass the image url from default page to html page, whlile html file loading only it has to show image and after sending that html mail image has to display in that mail. how to achieve this. reply me, thanks in advance.

                        V 1 Reply Last reply
                        0
                        • V ven753

                          Hi now i am trying to pass that image url from default page to html page as follows public string path = "images/s1.jpg"; and in html ![](Default.aspx?Code=<%= path%>) while loading only image has to display but i am not getting, blank image i am getting. even if i add source directly in html like below if i see in design view image will show and if i send that html file mail as shown below System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(); mail.From = new MailAddress("emailid"); mail.To.Add("emailid"); mail.IsBodyHtml = true; mail.Subject = "Policy Acceptance"; string FilePath = Server.MapPath("~/sendmail.htm"); FileStream f1 = new FileStream(FilePath, FileMode.Open); StreamReader sr = new StreamReader(f1); string str = sr.ReadToEnd(); str = str.Replace("[[[user]]]", "name"); f1.Close(); mail.Body = str; SmtpClient smtp = new SmtpClient("server ip", portnumber); smtp.Send(mail); after sending mail, mail will recieve but image will like blank. I need to pass the image url from default page to html page, whlile html file loading only it has to show image and after sending that html mail image has to display in that mail. how to achieve this. reply me, thanks in advance.

                          V Offline
                          V Offline
                          ven753
                          wrote on last edited by
                          #15

                          hi now i aded LinkedResource myimage = new LinkedResource(serpath); // Create HTML view AlternateView htmlMail = AlternateView.CreateAlternateViewFromString(str, null, "text/html"); // Set ContentId property. Value of ContentId property must be the same as // the src attribute of image tag in email body. myimage.ContentId = "companylogo"; htmlMail.LinkedResources.Add(myimage); mail.AlternateViews.Add(htmlMail); so it sorted out and image is embedding to HTML and is working from default page only by accessing html page. but one problem is again if i redraw or overwrite the image after mail sending i am trying to delete that file by using folowing code if (System.IO.File.Exists(serpath)) { System.IO.File.Delete(serpath); } but it is giving The process cannot access the file serpath because it is being used by another process. How to resolve this. Reply me , thanks in advance. if i create new image with different name means it creating and saving but once again if try for same image name am getting following error An exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll but was not handled in user code Additional information: A generic error occurred in GDI+. i need to overwrite same image also. how to resolve this Reply me , thanks in advance.

                          Richard DeemingR 1 Reply Last reply
                          0
                          • V ven753

                            hi now i aded LinkedResource myimage = new LinkedResource(serpath); // Create HTML view AlternateView htmlMail = AlternateView.CreateAlternateViewFromString(str, null, "text/html"); // Set ContentId property. Value of ContentId property must be the same as // the src attribute of image tag in email body. myimage.ContentId = "companylogo"; htmlMail.LinkedResources.Add(myimage); mail.AlternateViews.Add(htmlMail); so it sorted out and image is embedding to HTML and is working from default page only by accessing html page. but one problem is again if i redraw or overwrite the image after mail sending i am trying to delete that file by using folowing code if (System.IO.File.Exists(serpath)) { System.IO.File.Delete(serpath); } but it is giving The process cannot access the file serpath because it is being used by another process. How to resolve this. Reply me , thanks in advance. if i create new image with different name means it creating and saving but once again if try for same image name am getting following error An exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll but was not handled in user code Additional information: A generic error occurred in GDI+. i need to overwrite same image also. how to resolve this Reply me , thanks in advance.

                            Richard DeemingR Offline
                            Richard DeemingR Offline
                            Richard Deeming
                            wrote on last edited by
                            #16

                            Make sure you're calling Dispose on the MailMessage instance after you've sent it. The simplest approach is to wrap it in a using block:

                            using (MailMessage mail = new MailMessage())
                            {
                            ...
                            SmtpClient smtp = new SmtpClient();
                            smtp.Send(mail);
                            }


                            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups