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

  • Question

  • User-1574460096 posted

    Hello,

    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>
        <item...

    Thanks.

    Br.

    Marco.

    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,
                FileMode.OpenOrCreate);
                DataContractSerializer ser1 =
                    new DataContractSerializer(typeof(TestData));
                ser1.WriteObject(writer, t1);
                writer.Close();
    
    
                // Read that instance from and create an image
                FileStream fs = new FileStream(toFileName,
            FileMode.OpenOrCreate);
                DataContractSerializer ser2 =
                    new DataContractSerializer(typeof(TestData));
                // Deserialize the data and read it from the instance.
                TestData deserializedTestData = (TestData)ser2.ReadObject(fs);
                fs.Close();
    
    
    
                //read the unsigned byte array
                byte[] unsigned = (byte[])(Array)deserializedTestData.face;
    
                using (MemoryStream memstr = new MemoryStream(unsigned))
                {
                    Image img = Image.FromStream(memstr);
    
                    img.Save(toImageName);
                }
    
    
                /* XML Serializer - Second tests */
    
    
                // Read that instance from and create an image
                FileStream writer1 = new FileStream(toFileName1,
            FileMode.OpenOrCreate);
                XmlSerializer ser3 = new XmlSerializer(typeof(TestData));
                ser3.Serialize(writer1, t1);
                writer1.Close();
    
    
                // Read that instance from and create an image
                FileStream fs1 = new FileStream(toFileName1,
            FileMode.OpenOrCreate);
                XmlSerializer ser4 = new XmlSerializer(typeof(TestData));
                TestData deserializedTestData1 = (TestData)ser4.Deserialize(fs1);
    
                fs1.Close();
    
                
    
                byte[] unsigned1 = (byte[])(Array)deserializedTestData1.face;
    
                using (MemoryStream memstr = new MemoryStream(unsigned1))
                {
                    Image img = Image.FromStream(memstr);
    
                    img.Save(toImageName_xml);
                }
    
    
                Console.WriteLine("End...");
                Console.ReadKey();
    
            }
    
    
           
    
    
            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;
            }
    
            [DataContract()]
            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">
    <a:byte>-1</a:byte>
    <a:byte>-40</a:byte>
    <a:byte>-1</a:byte>
    <a:byte>-32</a:byte>
    <a:byte>0</a:byte>
    <a:....
    
    // The second XML serialized xml and  image created successfully as well
    
    <face>
        <byte>-1</byte>
        <byte>-40</byte>
        <byte>-1</byte>
        <byte>-32</byte>
        <byte>0</byte>
        <byte>16</byte>
    .....
    
    

     

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

    Best regards,

    Sean

    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) {
                    mailMessage.To.Add(item);
                }
                smtpClient.Send(mailMessage);

     Thanks.

    Br.

    Marco.

    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,

    Sean

    Wednesday, January 6, 2021 9:07 AM