【问题标题】:Image box flickers after new image is loaded加载新图像后图像框闪烁
【发布时间】:2015-02-14 23:39:07
【问题描述】:

所以我有这段代码,它为数组中的每个字符串添加文件路径并将其用作图像框的文件路径,这是代码:

 private async void Button7_Click(object sender, RoutedEventArgs e)
    {
        string[] images = new string[] { "Star_00001.png", "Star_00002.png", "Star_00003.png", "Star_00004.png", "Star_00005.png", "Star_00006.png", "Star_00007.png", "Star_00008.png"};

        string path = "Assets/Star/";

            foreach(string file in images)
            {

            string thepath = Path.Combine(path,file);
            await Task.Delay(46);
            BitmapImage Image = new BitmapImage();
            Image.UriSource = new Uri(this.BaseUri, thepath);
            StarImage.Source = Image;

            }
     }  

现在每次将新图像加载到 StarImage 时,它​​都会闪烁,据我所知,他们无法阻止这种情况,因为这是在图像框中加载新图像的效果,但是有人知道吗停止这种情况并产生动画效果的替代方法?

【问题讨论】:

  • 这真的是关于 WPF,还是您正在构建一个商店应用程序?
  • 我已经编辑了问题标签。 Windows 应用商店应用不使用 WPF。

标签: c# image xaml windows-runtime windows-store-apps


【解决方案1】:

我认为某种缓冲技术可以减少或消除您遇到的闪烁问题。

我从来没有在 c# 中这样做过,但本质上你绘制到一个尚未可见的图像,称为缓冲区,然后在图像完全绘制后使其可见。

This may help.

【讨论】:

  • 您是说将一些图像加载到不同的图像框并在使用时使图像框可见,并且在将图像加载到该图像框时将背景中的其他图像加载到另一个图像框和等等?
  • 基本上是的。我不确定如何在 C# 中执行此操作。寻找双缓冲或三缓冲的 C# 技术。
【解决方案2】:

试试这个:

 private async void Button7_Click(object sender, RoutedEventArgs e)
    {
        string[] images = new string[] { "Star_00001.png", "Star_00002.png", "Star_00003.png", "Star_00004.png", "Star_00005.png", "Star_00006.png", "Star_00007.png", "Star_00008.png" };

        string path = "Assets/Star/";

        foreach (string file in images)
        {

            string thepath = Path.Combine(path, file);
            await Task.Delay(46);

            BitmapImage Image = new BitmapImage();
            Image.BeginInit();
            Image.UriSource = new Uri(this.BaseUri, thepath);
            Image.CacheOption = BitmapCacheOption.OnLoad;
            Image.EndInit();

            StarImage.Source = Image;

        }
    }

【讨论】:

  • 使用 BeginInIt()、CacheOption、BitmapCacheOption.OnLoad 和 EndInit();它是在问我是否缺少参考资料?
  • 答案是(正确地)为 WPF 给出的。但是,它不起作用,因为问题实际上并不是针对 WPF,而是针对商店应用程序。
  • @Clemens Ahh 我明白了,这完全是我的坏家伙!你自己心里有什么答案吗?
  • 您可能会为 BitmapImage 的 ImageOpened 事件附加一个处理程序并在那里执行 StarImage.Source 分配。
【解决方案3】:

几个月来我一直在绞尽脑汁试图解决这个问题,然后我找到了这篇文章。它帮助我解决了这个问题!我终其一生都无法理解为什么微软不断改变语法来在 Windows 中做事。

How to set Background of a Button without flicker?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多