dotnet / dotnet/winforms

Icon.Save(stream) each time returns different results

Open
#8,853 13 comments 0 reactions 0 assignees View on GitHub
area-System.Drawing help wanted
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

### Description

We use the following code to compare two icons:

```
private static bool IconEquals(Icon icon1, Icon icon2) {
var stream1 = new System.IO.MemoryStream();
icon1.Save(stream1);
var stream2 = new System.IO.MemoryStream();
icon2.Save(stream2);
return System.Linq.Enumerable.SequenceEqual(stream1.ToArray(), stream2.ToArray());
}
```

With the net6.0 release the stream.ToArray() method each time returns different results for the same Icon.
You can modify the IconEquals as follows and it will return false:

```
private static bool IconEquals(Icon icon1) {
var stream1 = new System.IO.MemoryStream();
icon1.Save(stream1);
var stream2 = new System.IO.MemoryStream();
icon1.Save(stream2);
return System.Linq.Enumerable.SequenceEqual(stream1.ToArray(), stream2.ToArray());
}
```

### Reproduction Steps

Launch the following code with any Icon:
```
private static bool IconEquals(Icon icon1) {
var stream1 = new System.IO.MemoryStream();
icon1.Save(stream1);
var stream2 = new System.IO.MemoryStream();
icon1.Save(stream2);
return System.Linq.Enumerable.SequenceEqual(stream1.ToArray(), stream2.ToArray());
}
```

### Expected behavior

The IconEquals method should return true.

### Actual behavior

The IconEquals method returns false.

### Regression?

Works on net5.0

### Known Workarounds

_No response_

### Configuration

Windows 10

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.