title: Comparing Images as Byte Arrays for Unit Tests
body: <p>I have a class that creates an <code>Image</code> from a <code>Byte[]</code>, along with some other logic, and I am trying to write a unit test that asserts that the <code>Image</code> instance returned from my class is the same as some fake <code>Image</code> I have in my unit test.</p>
  
  <p>I cannot find a reliable way to:</p>
  
  <ul>
  <li>Start with a fake <code>Image</code> \ <code>Byte[]</code> \ Resource \ <em>something</em>.</li>
  <li>Pass a <code>Byte[]</code> representing the fake <em>thing</em> to my class.</li>
  <li>Assert the <code>Image</code> returned from my class is the same as my fake <em>thing</em>.</li>
  </ul>
  
  <p>Here's the code I have come up with so far:</p>
  
  <pre><code>Bitmap fakeBitmap = new Bitmap(1, 1);
  
  Byte[] expectedBytes;
  using (var ms = new MemoryStream())
  {
      fakeBitmap.Save(ms, ImageFormat.Png);
      expectedBytes = ms.ToArray();
  }
  
  //This is where the call to class goes
  Image actualImage;
  using (var ms = new MemoryStream(expectedBytes))
      actualImage = Image.FromStream(ms);
  
  Byte[] actualBytes;
  using (var ms = new MemoryStream())
  {
      actualImage.Save(ms, ImageFormat.Png);
      actualBytes = ms.ToArray();
  }
  
  var areEqual =
      Enumerable.SequenceEqual(expectedBytes, actualBytes);
  
  Console.WriteLine(areEqual);
  
  var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  
  using(StreamWriter sw = new StreamWriter(Path.Combine(desktop, "expectedBytes.txt")))
      sw.Write(String.Join(Environment.NewLine, expectedBytes));
  
  
  using(StreamWriter sw = new StreamWriter(Path.Combine(desktop, "actualBytes.txt")))
      sw.Write(String.Join(Environment.NewLine, actualBytes));
  </code></pre>
  
  <p>This always returns <code>false</code>.</p>