install-package communitytoolkit.mvvm;
private void InitImgSource()
{
var assembly = Assembly.GetExecutingAssembly();
//NameSpace.Folder.FileName
using (var stream = assembly.GetManifestResourceStream("WpfApp50.Images.1.jpg"))
{
if (stream != null)
{
BitmapImage bmi = new BitmapImage();
bmi.BeginInit();
bmi.StreamSource = stream;
bmi.CacheOption = BitmapCacheOption.OnLoad;
bmi.EndInit();
bmi.Freeze();
ElpImgSource = bmi;
}
}
}
![image]()
![image]()
![image]()
//xaml
<Window x:Class="WpfApp50.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:behavior="http://schemas.microsoft.com/xaml/behaviors"
WindowState="Maximized"
xmlns:local="clr-namespace:WpfApp50"
mc:Ignorable="d"
Title="{Binding TitleStr,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Height="450" Width="800">
<Grid>
<Ellipse Width="{Binding ElpWidth,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Height="{Binding ElpHeight,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
RenderTransformOrigin="0.5,0.5" >
<Ellipse.Fill>
<ImageBrush ImageSource="{Binding ElpImgSource,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
</Ellipse.Fill>
</Ellipse>
</Grid>
</Window>
//cs
using CommunityToolkit.Mvvm.ComponentModel;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace WpfApp50
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var vm = new MainVM(this);
this.DataContext=vm;
}
}
public partial class MainVM : ObservableObject
{
Window win;
public MainVM(Window winValue)
{
win=winValue;
if (win!=null)
{
win.Loaded+=Win_Loaded;
}
}
private void Win_Loaded(object sender, RoutedEventArgs e)
{
ElpWidth=win.ActualHeight;
ElpHeight=win.ActualHeight;
ElpImgSource= LoadImgSourceFromResourceFile();
System.Timers.Timer tmr = new System.Timers.Timer();
tmr.Elapsed+=Tmr_Elapsed;
tmr.Interval=1000;
tmr.Start();
}
int idx = 1;
private void Tmr_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
{
if (++idx>10)
{
idx=1;
}
string resourceFile = $"WpfApp50.Images.{idx}.jpg";
ElpImgSource=LoadImgSourceFromResourceFile(resourceFile);
}
private BitmapImage LoadImgSourceFromResourceFile(string resourceFileName = "WpfApp50.Images.1.jpg")
{
TitleStr= resourceFileName;
BitmapImage bmi = new BitmapImage();
var assembly = Assembly.GetExecutingAssembly();
//NameSpace.Folder.FileName
using (var stream = assembly.GetManifestResourceStream(resourceFileName))
{
if (stream != null)
{
bmi.BeginInit();
bmi.StreamSource = stream;
bmi.CacheOption = BitmapCacheOption.OnLoad;
bmi.EndInit();
bmi.Freeze();
}
}
return bmi;
}
[ObservableProperty]
private double elpWidth;
[ObservableProperty]
private double elpHeight;
[ObservableProperty]
private ImageSource elpImgSource;
[ObservableProperty]
private string titleStr;
}
}