简体   繁体   English

比较两个BitmapImages以检查它们在WPF中是否不同的最快方法

[英]Quickest way to compare two BitmapImages to check if they are different in WPF

What's the quickest way to compare 2 BitmapImage objects. 比较2个BitmapImage对象的最快方法是什么。 One is in the Image Source property, and another I create in code. 一个是在Image Source属性中,另一个是我在代码中创建的。

I can set the image source with the new bitmap image, but it causes flickering because it keeps setting the same image over and over. 我可以使用新的位图图像设置图像源,但它会导致闪烁,因为它会一遍又一遍地设置相同的图像。

I'd like to only set the image if its pixels are different from the one in Image.Source. 我想只设置图像,如果它的像素与Image.Source中的像素不同。

EDIT: 编辑:

AlbumArt is the Image in the view (following MVVM). AlbumArt是视图中的图像(跟随MVVM)。

Some code (running in the view code-behind): 一些代码(在视图代码后面运行):

Task.Factory.StartNew(() =>
    {
        while (((App)Application.Current).Running)
        {
            Thread.Sleep(1000);

            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                if ((this.DataContext as AudioViewModel).CurrentDevice != null)
                {
                    if ((((this.DataContext as AudioViewModel).CurrentDevice) as AUDIO).SupportsAlbumArt)
                    {
                        BitmapImage image = new BitmapImage();
                        image.BeginInit();
                        image.UriSource = new Uri((((this.DataContext as AudioViewModel).CurrentDevice) as AUDIO).AlbumArt);
                        image.CacheOption = BitmapCacheOption.None;
                        image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                        image.EndInit();

                        AlbumArt.Source = image;
                        ...

You could compare the bytes of the BitmapImage to check if they are equal 您可以比较BitmapImage的字节以检查它们是否相等

Something like: 就像是:

public static class BitmapImageExtensions
{
    public static bool IsEqual(this BitmapImage image1, BitmapImage image2)
    {
        if (image1 == null || image2 == null)
        {
            return false;
        }
        return image1.ToBytes().SequenceEqual(image2.ToBytes());
    }

    public static byte[] ToBytes(this BitmapImage image)
    {
        byte[] data = new byte[] { };
        if (image != null)
        {
            try
            {
                var encoder = new BmpBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(image));
                using (MemoryStream ms = new MemoryStream())
                {
                    encoder.Save(ms);
                    data = ms.ToArray();
                }
                return data;
            }
            catch (Exception ex)
            {
            }
        }
        return data;
    }
}

Usage: 用法:

BitmapImage image1 = ..............
BitmapImage image2 = ................

if (image1.IsEqual(image2))
{
    // same image
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM