Hi,
I cannot reproduce your problem with your code in my demo:
<Window x:Class="WpfApp1.Window044xaml"
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:local="clr-namespace:WpfApp044"
mc:Ignorable="d"
Title="sleepyVision_230304" Height="200" Width="400">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Image Source="{Binding PreviewImage}" Margin="10"/>
<Button Grid.Row="1" Content="Load" Command="{Binding}" Margin="5"/>
</Grid>
</Window>
ViewModel:
using System;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
namespace WpfApp044
{
public class ViewModel : INotifyPropertyChanged, ICommand
{
private ImageSource? _previewImage;
public ImageSource? PreviewImage
{
get => _previewImage;
set
{
_previewImage = value;
NotifyPropertyChanged(nameof(PreviewImage));
}
}
public bool CanExecute(object? parameter) => true;
public void Execute(object? parameter)
{
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
FormattedText text = new FormattedText(DateTime.Now.ToString("hh:mm:ss.fff"),
new CultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface(new FontFamily("Arial"), FontStyles.Normal, FontWeights.Normal, new FontStretch()),
30,
Brushes.Red);
DrawingVisual visual = new DrawingVisual();
DrawingContext drawingContext = visual.RenderOpen();
drawingContext.DrawText(text, new Point(2, 2));
drawingContext.Close();
int width = 300;
int height = 300;
var bitmap = new RenderTargetBitmap(
width,
height,
96,
96,
PixelFormats.Default);
bitmap.Render(visual);
PreviewImage = bitmap;
}
}
// INotifyPropertyChanged
#pragma warning disable CS8612 // Nullability of reference types in type doesn't match implicitly implemented member.
public event PropertyChangedEventHandler PropertyChanged;
public event EventHandler? CanExecuteChanged;
public void NotifyPropertyChanged([CallerMemberName] string info = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
}
}
Result: