0

在 c# 中,我们可以使用按像素比较图像,

        Bitmap b1 = new Bitmap(textBox1.Text);
        Bitmap b2 = new Bitmap(textBox2.Text);       
            for (int x = 0; x < b1.Width; x++)
            {
                for (int y = 0; y < b1.Height; y++)
                {
                    if (b1.GetPixel(x, y) != b2.GetPixel(x, y))
                    {
                        Console.Out.Write("Mismatch");
                    }
                }
            }

winRT 应用程序中的像素如何通过这种方式进行比较?

提前致谢

4

1 回答 1

1
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;

namespace App110
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            Test();
        }

        private async void Test()
        {
            // Comparing image to itself should return true
            Debug.Assert(await ComparePackageImages("Assets\\Logo.png", "Assets\\Logo.png"));

            // "Copy of Logo.png" is "Logo.png" with changed one pixel
            // and should return false when compared with "Logo.png"
            Debug.Assert(!await ComparePackageImages("Assets\\Logo.png", "Assets\\Copy of Logo.png"));

            // Two images of different size should 
            Debug.Assert(!await ComparePackageImages("Assets\\Logo.png", "Assets\\SmallLogo.png"));

            await new MessageDialog("Success!").ShowAsync();
            Application.Current.Exit();
        }

        private async Task<bool> ComparePackageImages(string packageFilePath1, string packageFilePath2)
        {
            var b1 = await LoadWriteableBitmapFromPackageFilePath(packageFilePath1);
            var b2 = await LoadWriteableBitmapFromPackageFilePath(packageFilePath2);

            var pixelStream1 = b1.PixelBuffer.AsStream();
            var pixelStream2 = b2.PixelBuffer.AsStream();

            return StreamEquals(pixelStream1, pixelStream2);
        }

        private static async Task<WriteableBitmap> LoadWriteableBitmapFromPackageFilePath(string packageFilePath1)
        {
            var bitmap = new WriteableBitmap(1, 1);
            var storageFile = await Package.Current.InstalledLocation.GetFileAsync(packageFilePath1);

            using (var streamWithContentType = await storageFile.OpenReadAsync())
            {
                await bitmap.SetSourceAsync(streamWithContentType);
            }

            return bitmap;
        }

        static bool StreamEquals(Stream stream1, Stream stream2)
        {
            const int bufferSize = 2048;
            byte[] buffer1 = new byte[bufferSize]; //buffer size
            byte[] buffer2 = new byte[bufferSize];

            while (true)
            {
                int count1 = stream1.Read(buffer1, 0, bufferSize);
                int count2 = stream2.Read(buffer2, 0, bufferSize);

                if (count1 != count2)
                    return false;

                if (count1 == 0)
                    return true;

                // You might replace the following with an efficient "memcmp"
                if (!buffer1.Take(count1).SequenceEqual(buffer2.Take(count2)))
                    return false;
            }
        }
    }
}

流比较由比较 C# 中的二进制文件提供。

位图访问代码片段由WinRT XAML Toolkit提供。

于 2013-02-12T07:25:09.957 回答