比较两个BitmapImage的最快方法,以检查它们在WPF中是否不同 [英] Quickest way to compare two BitmapImages to check if they are different in WPF

查看:159
本文介绍了比较两个BitmapImage的最快方法,以检查它们在WPF中是否不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比较2个BitmapImage对象的最快方法是什么.一个在Image Source属性中,另一个在代码中创建.

What's the quickest way to compare 2 BitmapImage objects. One is in the Image Source property, and another I create in code.

我可以使用新的位图图像设置图像源,但是由于不断重复设置相同的图像,因此会引起闪烁.

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.

我只想设置图像的像素与Image.Source中的像素不同的图像.

I'd like to only set the image if its pixels are different from the one in Image.Source.

AlbumArt是视图中的图像(在MVVM之后).

AlbumArt is the Image in the view (following 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;
                        ...

推荐答案

您可以比较BitmapImage的字节以检查它们是否相等

You could compare the bytes of the BitmapImage to check if they are equal

类似的东西:

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;
    }
}

用法:

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

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

这篇关于比较两个BitmapImage的最快方法,以检查它们在WPF中是否不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆