'Too many bytes in what should have been a 7 bit encoded Int32' and idk what I'm doing wrong...

What am I doing wrong? everything is as it should. I’m basically reading and writing a dictionary to the filesystem, and populating some RawImages with the values of the dictionary. Can someone shed some light?

 static void Serialize(string path, Dictionary<string, byte[]> data)
        {
            using (var file = File.Create(path))
            using (var deflate = new DeflateStream(file, CompressionMode.Compress))
            using (var writer = new BinaryWriter(deflate))
            {
                writer.Write(data.Count);
                foreach(var pair in data)
                {
                    writer.Write(pair.Key);
                    writer.Write(pair.Value);                    
                }
                writer.Close();
            }
        }
        static Dictionary<string,byte[]> Deserialize(string path)
        {
            using (var file = File.OpenRead(path))
            using (var deflate = new DeflateStream(file, CompressionMode.Decompress))
            using (var reader = new BinaryReader(deflate))
            {
                int count = reader.ReadInt32();
                var data = new Dictionary<string, byte[]>();
                while(count-->0) {
                    data.Add(reader.ReadString(), reader.ReadBytes(count));
                }
                reader.Close();
                return data;
            }
        }

You need to record the length of the pair.Value byte so you know how much data to read back out. So in your Serialize method, you would change your loop to be something like this:

foreach(var pair in data)
{
    writer.Write(pair.Key);
    writer.Write(pair.Value.Length);
    writer.Write(pair.Value);
}

Then in your Deserialize, when you read the data:

int count = reader.ReadInt32();
var dict = new Dictionary<string, byte[]>(count); //initialize capacity, too

for(int i = 0; i < count; i++)
{
    var key = reader.ReadString();
    var len = reader.ReadInt32();

    dict.Add(key, reader.ReadBytes(len));
}