BitMiracle.LibTiff.Classic.FieldValue.ToByteArray C# (CSharp) Method

ToByteArray() public method

Retrieves value converted to array of bytes.

If value is array of bytes then it retrieved unaltered.

If value is array of short, ushort, int or uint values then each element of field value gets converted to byte and added to resulting array.

If value is string then it gets converted to byte[] using Latin1 encoding encoder.

If value is of any other type then null is returned.

public ToByteArray ( ) : byte[]
return byte[]
        public byte[] ToByteArray()
        {
            if (m_value == null)
                return null;

            Type t = m_value.GetType();
            if (t.IsArray)
            {
                if (m_value is byte[])
                    return m_value as byte[];
                else if (m_value is short[])
                {
                    short[] temp = m_value as short[];
                    byte[] result = new byte[temp.Length];
                    for (int i = 0; i < temp.Length; i++)
                        result[i] = (byte)temp[i];

                    return result;
                }
                else if (m_value is ushort[])
                {
                    ushort[] temp = m_value as ushort[];
                    byte[] result = new byte[temp.Length];
                    for (int i = 0; i < temp.Length; i++)
                        result[i] = (byte)temp[i];

                    return result;
                }
                else if (m_value is int[])
                {
                    int[] temp = m_value as int[];
                    byte[] result = new byte[temp.Length];
                    for (int i = 0; i < temp.Length; i++)
                        result[i] = (byte)temp[i];

                    return result;
                }
                else if (m_value is uint[])
                {
                    uint[] temp = m_value as uint[];
                    byte[] result = new byte[temp.Length];
                    for (int i = 0; i < temp.Length; i++)
                        result[i] = (byte)temp[i];

                    return result;
                }
            }
            else if (m_value is string)
                return Tiff.Latin1Encoding.GetBytes(m_value as string);

            return null;
        }