How to elaborate Json sbyte[] obj to get an image. RRS feed

  • Question

  • User-1574460096 posted


    I'm receiving a Json image in this format below I dont have any other chance to get it in sbyte[] (because it has negative values, I think). So I would like to know how could I get the image and display it and / or save as a File.

    [DataMember(Order = 1, IsRequired = true)]
            //public List<sbyte> img { get; set; }
            public sbyte[] face{ get; set; }

    <face type="array">
        <item type="number">-1</item>
        <item type="number">-40</item>
        <item type="number">-1</item>
        <item type="number">-32</item>




    Saturday, January 2, 2021 10:31 AM

All replies

  • User-1330468790 posted

    Hi Dunder,


    Basically, you could convert the sbyte array to byte array and get image from that byte array if you want to use the Class System.Drawing.Image.

    I did a test and confirmed it is feasible.


    More details, you could refer to below codes.

    public static void Main(string[] args)
                Image image = Image.FromFile(@"C:\Path\To\Original.jpg");
                string toFileName = @"C:\Path\To\TheFirstSerialization.xml";
                string toFileName1 = @"C:\Path\To\TheSecondSerialization.xml";
                string toImageName = @"C:\Path\To\TheFirstGenerated.jpg";
                string toImageName_xml = @"C:\Path\To\TheSecondGenerated.jpg";
                // Original bytes and Image and TestData
                byte[] bytes = imageToByteArray(image);
                sbyte[] signed = (sbyte[])(Array)bytes;
                TestData t1 = new TestData()
                    face = signed
                /* DataContract - First tests */
                // Create a new instance of the test data class.
                FileStream writer = new FileStream(toFileName,
                DataContractSerializer ser1 =
                    new DataContractSerializer(typeof(TestData));
                ser1.WriteObject(writer, t1);
                // Read that instance from and create an image
                FileStream fs = new FileStream(toFileName,
                DataContractSerializer ser2 =
                    new DataContractSerializer(typeof(TestData));
                // Deserialize the data and read it from the instance.
                TestData deserializedTestData = (TestData)ser2.ReadObject(fs);
                //read the unsigned byte array
                byte[] unsigned = (byte[])(Array)deserializedTestData.face;
                using (MemoryStream memstr = new MemoryStream(unsigned))
                    Image img = Image.FromStream(memstr);
                /* XML Serializer - Second tests */
                // Read that instance from and create an image
                FileStream writer1 = new FileStream(toFileName1,
                XmlSerializer ser3 = new XmlSerializer(typeof(TestData));
                ser3.Serialize(writer1, t1);
                // Read that instance from and create an image
                FileStream fs1 = new FileStream(toFileName1,
                XmlSerializer ser4 = new XmlSerializer(typeof(TestData));
                TestData deserializedTestData1 = (TestData)ser4.Deserialize(fs1);
                byte[] unsigned1 = (byte[])(Array)deserializedTestData1.face;
                using (MemoryStream memstr = new MemoryStream(unsigned1))
                    Image img = Image.FromStream(memstr);
            public static byte[] imageToByteArray(Image imageIn)
                MemoryStream ms = new MemoryStream();
                imageIn.Save(ms, imageIn.RawFormat);
                return ms.ToArray();
            public static Image byteArrayToImage(byte[] byteArrayIn)
                MemoryStream ms = new MemoryStream(byteArrayIn);
                Image returnImage = Image.FromStream(ms);
                return returnImage;
            public class TestData
                [DataMember(Order = 1, IsRequired = true)]
                public sbyte[] face { get; set; }

     Test xml:

    // The first DataContract xml and image created successfully
    <face xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    // The second XML serialized xml and  image created successfully as well


    If you still have problems related to this topic, feel free to let me know.

    Best regards,


    Monday, January 4, 2021 8:56 AM
  • User-464022988 posted

    Hi Sean, 

    thanks for your replay, it works like a charme :-) ... I would like just ask you why I can get image in the email body below in my PC outlook client, but not in the Phone outlook client or my gmail web view. Again, is it necessary to save always the image captured to pass it in the html body of the email ?

    var smtpClient = new SmtpClient();

                var mailMessage = new MailMessage
                    From = new MailAddress("fever_detection@gknpm.com"),
                    Subject = "Fever Detection Kiosk",
                    Body = "<h1>High temperature detected: "+dataObject.temperature+"</h1>"
                         + "<br/>"
                         + "<img src=\""+toImageName+"\" alt=\""+dataObject.name+"\">",
                    IsBodyHtml = true,
                foreach (string item in dataObject.toEmails) {




    Monday, January 4, 2021 11:15 AM
  • User-1330468790 posted

    Hi Mameli,


    but not in the Phone outlook client or my gmail web view.

    Accoring to your codes, you add standard html codes in the body to display the image. However, Phone outlook client or gmail web view may not behave like a browser. 

    I am not familiar with the Phone outlook client but if you want to fix the image issue in gmail web view, you might need to need to modify the image tag because Google only allows images which are coming from trusted source. 

    • Including the scheme in the src url (using "//" does not work - use full scheme EG: "https://")
    • Including width and height attributes
    • Including style="display:block" attribute
    • Including both alt and title attributes

    For example,

    <img src="https://static.mydomain.com/images/logo.png" alt="Logo" title="Logo" style="display:block" width="200" height="87" />


    Hope helps.

    Best regards,


    Wednesday, January 6, 2021 9:07 AM