当前位置:   article > 正文

WPF 通过Image控件实现多张图片的播放

wpf image控件显示多张图片

开发环境建议使用VS2010及以上

效果:

 

xaml代码:

  1. <Window
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:PictureTest" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" mc:Ignorable="d"
  5. x:Class="PictureTest.MainWindow"
  6. x:Name="Window"
  7. Title="MainWindow"
  8. Width="640" Height="480">
  9. <Grid x:Name="LayoutRoot">
  10. <Image Name="image1" Height="429" HorizontalAlignment="Left" Margin="96,10,0,0" Source="{Binding Path=ImageFullPath}" Stretch="Fill" VerticalAlignment="Top" Width="302" />
  11. </Grid>
  12. </Window>

C#代码(后端代码)

  1. using System;
  2. using System.Windows;
  3. using System.ComponentModel;
  4. namespace PictureTest
  5. {
  6. /// <summary>
  7. /// MainWindow.xaml 的交互逻辑
  8. /// </summary>
  9. public class GenerateRandomImagepath : INotifyPropertyChanged
  10. {
  11. public event PropertyChangedEventHandler PropertyChanged;
  12. private void OnPropertyChanged(string propertyName)
  13. {
  14. if (PropertyChanged != null)
  15. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  16. }
  17. private string _imageFullPath;
  18. Random random = new Random((int)DateTime.Now.Ticks);
  19. readonly int Minint;
  20. readonly int Maxint;
  21. readonly string PrefixImageName;
  22. readonly string ImageExtension;
  23. /// <summary>
  24. /// Used in data Binding
  25. /// </summary>
  26. public string ImageFullPath { get{return _imageFullPath;}
  27. set
  28. {
  29. _imageFullPath = value;
  30. OnPropertyChanged("ImageFullPath");
  31. }
  32. }
  33. public GenerateRandomImagepath(string prefixName, string extension)
  34. {
  35. this.PrefixImageName = prefixName;
  36. this.Minint = 1;
  37. this.Maxint = 6;
  38. this.ImageExtension = extension;
  39. }
  40. int RandomNumber()
  41. {
  42. return random.Next(this.Minint, this.Maxint);
  43. }
  44. public void GenerateNewRandomImagePath()
  45. {
  46. this.ImageFullPath = this.PrefixImageName + RandomNumber() + this.ImageExtension;
  47. }
  48. }
  49. public partial class MainWindow : Window
  50. {
  51. GenerateRandomImagepath RandomImagePath;
  52. System.Timers.Timer timer = new System.Timers.Timer(2000);
  53. public MainWindow()
  54. {
  55. this.InitializeComponent();
  56. // 在此点下面插入创建对象所需的代码。
  57. RandomImagePath = new GenerateRandomImagepath(@"C:\Users\Administrator\Documents\Expression\Blend 4\Projects\PictureTest\PictureTest\Image\", @".jpg");
  58. timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
  59. timer.Enabled = true;
  60. //timer.Start();
  61. this.DataContext = RandomImagePath;
  62. RandomImagePath.GenerateNewRandomImagePath();
  63. }
  64. void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  65. {
  66. RandomImagePath.GenerateNewRandomImagePath();
  67. }
  68. }
  69. }

 

转载于:https://my.oschina.net/u/2525682/blog/699941

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/732003
推荐阅读
相关标签
  

闽ICP备14008679号