Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BitmapTests
- {
- [Test]
- public void CompareSameTest()
- {
- using (var first = new Bitmap(@"C:\temp\Blank.bmp"))
- {
- using (var other = new Bitmap(@"C:\temp\Blank.bmp"))
- {
- Assert.IsFalse(first == other);
- }
- }
- }
- [TestCase(@"C:\temp\Blank.bmp", @"C:\temp\Blank.bmp", true)]
- public void CompareBitmapsTest(string firstPath, string otherPath, bool expected)
- {
- using (var first = new Bitmap(firstPath))
- {
- using (var other = new Bitmap(otherPath))
- {
- Assert.AreEqual(expected, BitmapsEqual(first, other));
- }
- }
- }
- public static bool BitmapsEqual(Bitmap source, Bitmap other)
- {
- if (source == other) // Compare references
- {
- return true;
- }
- if (source.Size != other.Size)
- {
- throw new ArgumentException("Cannont compare images of different sizes!");
- }
- using (var sourceHelper = new BitMapHelper(source))
- {
- using (var otherHelper = new BitMapHelper(other))
- {
- return sourceHelper.Bytes.SequenceEqual(otherHelper.Bytes);
- }
- }
- }
- }
- public class BitMapHelper : IDisposable
- {
- private readonly Bitmap _bitmap;
- private BitmapData _bitmapData;
- public byte[] Bytes { get; private set; }
- private int _byteCount;
- public BitMapHelper(Bitmap bitmap)
- {
- _bitmap = bitmap;
- _byteCount = (bitmap.Width + 1)*(bitmap.Height + 1)*Image.GetPixelFormatSize(bitmap.PixelFormat)/8;
- Bytes = new byte[_byteCount];
- _bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
- System.Runtime.InteropServices.Marshal.Copy(_bitmapData.Scan0,Bytes,0,_byteCount);
- }
- public void Dispose()
- {
- _bitmap.UnlockBits(_bitmapData);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement