I am trying to load and display an image using WPF, but it does not work.
public partial class MainWindow : Window
{
BitmapImage imgsrc;
public MainWindow()
{
InitializeComponent();
imgsrc = new BitmapImage();
imgsrc.BeginInit();
imgsrc.UriSource = new Uri("c.jpg", UriKind.Relative);
imgsrc.EndInit();
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
drawingContext.DrawImage(imgsrc, new Rect(10, 10, 100, 100));
}
}
the c.jpg file is in the project and marked for copy to output.
the applications runs without errors and shows a white empty window
This is a known issue with overriding
OnRender()on aWindow.Don’t derive from
Window, useFrameworkElementinstead, or if you must useWindowtry setting the background to transparent.