3

I'm not sure if I've made anything wrong but I found Windows 8 has breaking changes when I'm using some simplest features in .NET framework. One of my machine is Windows 7 X64 with Visual Studio 2010 premium, the other is Windows 8 X64 with exactly the same Visual Studio. Both of the Win7 / Win8 system are downloaded as MSDN subscriber so they are all official. However for the following code:

static void Main(string[] args)
{
    byte[] dataBytes = new byte[256 * 256 * 4 + 256];
    MemoryStream resultStream = new MemoryStream();
    DeflateStream deflateStream = new DeflateStream(resultStream, CompressionMode.Compress);
    deflateStream.Write(dataBytes, 0, dataBytes.Length);
    Console.WriteLine(resultStream.Length);//2330

    Bitmap a = new Bitmap(256, 256);
    MemoryStream memoryStream1 = new MemoryStream();
    a.Save(memoryStream1, ImageFormat.Png);
    byte[] byteArray1 = memoryStream1.ToArray();
    Console.WriteLine(byteArray1.Length);//1275

    Console.Read();
}

it returns 2330/1275 on Window 7 but returns 0/384 on Windows 8. the codes are identical and are both under .NET Framework 4 Client Profile.

So I did anything wrong or it is a breaking change on Windows 8?

Thanks very much in advance.


Thanks for all your help guys. For the first case I tried the following code

        byte[] dataBytes = new byte[256 * 256 * 4 + 256];
        MemoryStream resultStream = new MemoryStream();
        DeflateStream deflateStream = new DeflateStream(resultStream, CompressionMode.Compress);
        deflateStream.Write(dataBytes, 0, dataBytes.Length);
        deflateStream.Close();
        Console.WriteLine(resultStream.ToArray().Length);

and I got 2338 on Windows7 and 271 on Windows8. So seems Windows 8 does have done some optimizations to make the result stream smaller.

user901938
  • 31
  • 2

1 Answers1

2

You should close and dispose the compressor stream to be sure that it has written all the data to the underlying stream.

Try this one on both systems:

byte[] dataBytes = new byte[256 * 256 * 4 + 256];

using(MemoryStream resultStream = new MemoryStream())
{
    using(DeflateStream deflateStream = new DeflateStream(resultStream, CompressionMode.Compress)
        deflateStream.Write(dataBytes, 0, dataBytes.Length);

    Console.WriteLine(resultStream.Length); // ?
}

The second code part must be having PNG compression difference on the other system. Likely working better jugding by the 1275 -> stream size downswing.

Community
  • 1
  • 1
AgentFire
  • 8,944
  • 8
  • 43
  • 90