[C#]幾種Bitmap比較方式

來源:互聯網
上載者:User

標籤:style   blog   http   color   width   os   

這裡選取圖片,規格如下:

大小:

關鍵代碼:

using System;using System.Diagnostics;using System.Drawing;using System.Drawing.Imaging;using System.IO;using System.Runtime.InteropServices;namespace ConsoleApplication27{    class Program    {        static void Main(string[] args)        {            try            {                Stopwatch _watch = new Stopwatch();                _watch.Start();                bool _compareByMenCmpResult = Resource1.cat.CompareByMemCmp(Resource1.cat);                _watch.Stop();                Console.WriteLine(string.Format("CompareByMemCmp: {0} {1}", _compareByMenCmpResult, _watch.ElapsedMilliseconds));                _watch.Reset();                _watch.Start();                bool _compareByPixel = Resource1.cat.CompareByPixel(Resource1.cat);                _watch.Stop();                Console.WriteLine(string.Format("CompareByPixel: {0} {1}", _compareByPixel, _watch.ElapsedMilliseconds));                _watch.Reset();                _watch.Start();                bool _compareByBase64String = Resource1.cat.CompareByBase64String(Resource1.cat);                _watch.Stop();                Console.WriteLine(string.Format("CompareByBase64String: {0} {1}", _compareByBase64String, _watch.ElapsedMilliseconds));                _watch.Reset();                _watch.Start();                bool _compareByArray = Resource1.cat.CompareByArray(Resource1.cat);                _watch.Stop();                Console.WriteLine(string.Format("CompareByArray: {0} {1}", _compareByArray, _watch.ElapsedMilliseconds));            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);            }            finally            {                Console.ReadLine();            }        }    }    public static class ImageTool    {        [DllImport("msvcrt.dll")]        private static extern int memcmp(IntPtr b1, IntPtr b2, long count);        /// <summary>        /// Bitmap比較        /// </summary>        /// <param name="b1">Bitmap1</param>        /// <param name="b2">Bitmap2</param>        /// <returns>比較結果</returns>        public static bool CompareByMemCmp(this Bitmap b1, Bitmap b2)        {            /*說明             *參考連結:             *http://stackoverflow.com/questions/2031217/what-is-the-fastest-way-i-can-compare-two-equal-size-bitmaps-to-determine-whethe             */            if ((b1 == null) != (b2 == null)) return false;            if (b1.Size != b2.Size) return false;            BitmapData _bdata1 = b1.LockBits(new Rectangle(new Point(0, 0), b1.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);            BitmapData _bdata2 = b2.LockBits(new Rectangle(new Point(0, 0), b2.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);            try            {                IntPtr _bd1scan0 = _bdata1.Scan0;                IntPtr _bd2scan0 = _bdata2.Scan0;                int _stride = _bdata1.Stride;                int _len = _stride * b1.Height;                return memcmp(_bd1scan0, _bd2scan0, _len) == 0;            }            finally            {                b1.UnlockBits(_bdata1);                b2.UnlockBits(_bdata2);            }        }        /// <summary>        /// 通過比較bitmap兩者ToBase64String來判斷是否相等        /// </summary>        /// <param name="b1">Bitmap1</param>        /// <param name="b2">Bitmap2</param>        /// <returns>比較結果</returns>        public static bool CompareByBase64String(this Bitmap b1, Bitmap b2)        {            /*說明            *參考連結:            *http://blogs.msdn.com/b/domgreen/archive/2009/09/06/comparing-two-images-in-c.aspx            */            string _b1Base64String, _b2Base64String;            MemoryStream _ms = new MemoryStream();            try            {                b1.Save(_ms, ImageFormat.Png);                _b1Base64String = Convert.ToBase64String(_ms.ToArray());                _ms.Position = 0;                b2.Save(_ms, ImageFormat.Png);                _b2Base64String = Convert.ToBase64String(_ms.ToArray());            }            finally            {                _ms.Close();            }            return _b1Base64String.Equals(_b2Base64String);        }        /// <summary>        /// 通過比較bitmap兩者像素顏色來判斷兩者是否相等        /// </summary>        /// <param name="b1">Bitmap1</param>        /// <param name="b2">Bitmap2</param>        /// <returns>比較結果</returns>        public static bool CompareByPixel(this Bitmap b1, Bitmap b2)        {            /*說明             *參考連結:             *http://blogs.msdn.com/b/domgreen/archive/2009/09/06/comparing-two-images-in-c.aspx             */            bool _flag = false;            if (b1.Width == b2.Width && b1.Height == b2.Height)            {                _flag = true;                Color _pixel1;                Color _pixel2;                for (int i = 0; i < b1.Width; i++)                {                    for (int j = 0; j < b1.Height; j++)                    {                        _pixel1 = b1.GetPixel(i, j);                        _pixel2 = b2.GetPixel(i, j);                        if (_pixel1 != _pixel2)                        {                            _flag = false;                            break;                        }                    }                }            }            return _flag;        }        /// <summary>        /// memcmp API        /// </summary>        /// <param name="b1">位元組數組1</param>        /// <param name="b2">位元組數組2</param>        /// <returns>如果兩個數組相同,返回0;如果數組1小於數組2,返回小於0的值;如果數組1大於數組2,返回大於0的值。</returns>        [DllImport("msvcrt.dll")]        private static extern IntPtr memcmp(byte[] b1, byte[] b2, IntPtr count);        /// <summary>        /// 通過比較bitmap兩者byte[]來判斷是否相等        /// </summary>        /// <param name="b1">Bitmap1</param>        /// <param name="b2">Bitmap2</param>        /// <returns>比較結果</returns>        public static bool CompareByArray(this Bitmap b1, Bitmap b2)        {            /*說明            *參考連結:            *http://www.cnblogs.com/zgqys1980/archive/2009/07/13/1522546.html            */            IntPtr _result = new IntPtr(-1);            MemoryStream _ms = new MemoryStream();            try            {                b1.Save(_ms, ImageFormat.Png);                byte[] _b1Array = _ms.ToArray();                _ms.Position = 0;                b2.Save(_ms, ImageFormat.Png);                byte[] _b2Array = _ms.ToArray();                _result = memcmp(_b1Array, _b2Array, new IntPtr(_b1Array.Length));            }            finally            {                _ms.Close();            }            return _result.ToInt32() == 0;        }    }}
測試結果:

希望有所協助!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.