C# - Cast a byte array to an array of struct and vice-versa (reverse)

后端 未结 5 1148
刺人心
刺人心 2021-01-19 17:05

I would like to save a Color[] to a file. To do so, I found that saving a byte array to a file using \"System.IO.File.WriteAllBytes\" should be very efficient.

I wou

5条回答
  •  梦谈多话
    2021-01-19 17:44

    Working code for reference (take care, I did not need the alpha channel in my case):

    // ************************************************************************
    // If someday Microsoft make Color serializable ...
        //public static void SaveColors(Color[] colors, string path)
        //{
        //  BinaryFormatter bf = new BinaryFormatter();
        //  MemoryStream ms = new MemoryStream();
        //  bf.Serialize(ms, colors);
        //  byte[] bytes = ms.ToArray();
        //  File.WriteAllBytes(path, bytes);
        //}
    
    // If someday Microsoft make Color serializable ...
        //public static Colors[] LoadColors(string path)
        //{
        //  Byte[] bytes = File.ReadAllBytes(path);
        //  BinaryFormatter bf = new BinaryFormatter();
        //  MemoryStream ms2 = new MemoryStream(bytes);
        //  return (Colors[])bf.Deserialize(ms2);
        //}
    
        // ******************************************************************
        public static void SaveColorsToFile(Color[] colors, string path)
        {
            var formatter = new BinaryFormatter();
    
            int count = colors.Length;
    
            using (var stream = File.OpenWrite(path))
            {
                formatter.Serialize(stream, count);
    
                for (int index = 0; index < count; index++)
                {
                    formatter.Serialize(stream, colors[index].R);
                    formatter.Serialize(stream, colors[index].G);
                    formatter.Serialize(stream, colors[index].B);
                }
            }
        }
    
        // ******************************************************************
        public static Color[] LoadColorsFromFile(string path)
        {
            var formatter = new BinaryFormatter();
    
            Color[] colors;
    
            using (var stream = File.OpenRead(path))
            {
                int count = (int)formatter.Deserialize(stream);
                colors = new Color[count];
    
                for (int index = 0; index < count; index++)
                {
                    byte r = (byte)formatter.Deserialize(stream);
                    byte g = (byte)formatter.Deserialize(stream);
                    byte b = (byte)formatter.Deserialize(stream);
    
                    colors[index] = Color.FromRgb(r, g, b);
                }
            }
    
            return colors;
        }
    
        // ******************************************************************
    

提交回复
热议问题