如何在WPF中将多个PNG图像呈现为GIF?

huangapple go评论103阅读模式
英文:

How to present multiple PNG images as GIF in wpf?

问题

我正在研究一个WPF项目。
我有40个带有透明背景的PNG文件,当我将它们拼在一起时,它们就像一个自然的加载条一样工作。我不想将它制作成GIF,而是像以PNG格式加载UI中的某些内容。

我尝试过的方法如下:

  1. 使用UI线程进行重复实现:我发现资源消耗非常大,被认为不适合使用。
  2. WpfAnimatedGif库:被认为不适合使用所需的40个PNG文件。

如果您有任何好的想法,请回复。

英文:

I studying a wpf project.
I have 40 PNG files with transparent backgrounds, and when I stitch them together, they work like a natural loading bar. I don't want to make it into a GIF, but rather like loading something into the UI in PNG format.

The attempts I've made are as follows:

  1. Repetitive implementation using UI Thread: I found a huge consumption of resources. It is considered unsuitable.
  2. WpfAnimatedGif library: It is considered inappropriate to use the desired 40 PNG files.

If you have any good ideas, please reply.

答案1

得分: 0

假设您在XAML中有一个名为imageImage元素,并且您的PNG文件的路径存储在名为imagePathsList<string>中,您可以使用一个定时器:

  1. private void UpdateLoadingBar()
  2. {
  3. var frameIndex = 0;
  4. var frameCount = imagePaths.Count;
  5. var timer = new DispatcherTimer();
  6. timer.Interval = TimeSpan.FromMilliseconds(100);
  7. timer.Tick += (sender, e) =>
  8. {
  9. loadingImage.Source = new BitmapImage(
  10. new Uri(imagePaths[frameIndex], UriKind.RelativeOrAbsolute));
  11. if (++frameIndex >= frameCount)
  12. {
  13. // Stop the timer when all frames have been displayed
  14. timer.Stop();
  15. }
  16. };
  17. timer.Start();
  18. }

或者,如果您熟悉异步编程,您可以使用:

  1. private async Task UpdateLoadingBar()
  2. {
  3. foreach (var imagePath in imagePaths)
  4. {
  5. loadingImage.Source = new BitmapImage(
  6. new Uri(imagePath, UriKind.RelativeOrAbsolute));
  7. // Delay between each frame (adjust the duration as needed)
  8. await Task.Delay(100);
  9. }
  10. }

这需要从异步事件处理程序中启动,例如:

  1. private async void StartLoadingButton_Click(object sender, RoutedEventArgs e)
  2. {
  3. await UpdateLoadingBar();
  4. }
英文:

Assuming you have in your XAML an Image element named image and your paths to the png files in a List<string> named imagePaths you can use a timer:

  1. private void UpdateLoadingBar()
  2. {
  3. var frameIndex = 0;
  4. var frameCount = imagePaths.Count;
  5. var timer = new DispatcherTimer();
  6. timer.Interval = TimeSpan.FromMilliseconds(100);
  7. timer.Tick += (sender, e) =>
  8. {
  9. loadingImage.Source = new BitmapImage(
  10. new Uri(imagePaths[frameIndex], UriKind.RelativeOrAbsolute));
  11. if (++frameIndex >= frameCount)
  12. {
  13. // Stop the timer when all frames have been displayed
  14. timer.Stop();
  15. }
  16. };
  17. timer.Start();
  18. }

Or if you are familiar with async programming you could use

  1. private async Task UpdateLoadingBar()
  2. {
  3. foreach (var imagePath in imagePaths)
  4. {
  5. loadingImage.Source = new BitmapImage(
  6. new Uri(imagePath, UriKind.RelativeOrAbsolute));
  7. // Delay between each frame (adjust the duration as needed)
  8. await Task.Delay(100);
  9. }
  10. }

This needs to be started from an async event handler like:

  1. private async void StartLoadingButton_Click(object sender, RoutedEventArgs e)
  2. {
  3. await UpdateLoadingBar();
  4. }

huangapple
  • 本文由 发表于 2023年6月19日 11:03:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定