How can I have a background image in the editor? Since there is no built-in support I thought I could simply add an adornment following the watermark example in the demo application but with an image instead of a textbox. Works pretty well but unfortunately it doesn't if the image is too big / small. It's never properly stretched the way that it fills the screen while respecting the aspect ratio.
This is my modified code:
private void OnViewTextAreaLayout(object sender, TextViewTextAreaLayoutEventArgs e)
{
// Get the horizontal scroll
double firstVisibleX = (e.View is IEditorView view ? view.ScrollState.HorizontalAmount : 0.0);
// Determine the center of the text area viewport
Rect textAreaViewportBounds = e.View.TextAreaViewportBounds;
Point center = new Point(firstVisibleX + textAreaViewportBounds.Width / 2 / e.View.SyntaxEditor.ZoomLevelAnimated, textAreaViewportBounds.Height / 2 / e.View.SyntaxEditor.ZoomLevelAnimated);
// Determine the center of the watermark element
UIElement element = watermarkAdornment.VisualElement;
if (!(element is Image image))
return;
Point textBlockCenter = new Point(image.Source.Width / 2, image.Source.Height / 2);
// Adjust scale
var scaleTrans = (ScaleTransform)image.RenderTransform;
scaleTrans.ScaleX = 1 / e.View.SyntaxEditor.ZoomLevelAnimated;
scaleTrans.ScaleY = 1 / e.View.SyntaxEditor.ZoomLevelAnimated;
// Determine the watermark location
Point watermarkLocation = new Point(center.X - textBlockCenter.X, center.Y - textBlockCenter.Y);
// Set the watermark location
watermarkAdornment.Location = watermarkLocation;
}
private IAdornment CreateWatermarkAdornment()
{
Image image = new Image()
{
Source = new BitmapImage(new Uri("C:\\test.jpg")),
IsHitTestVisible = false,
Stretch = Stretch.UniformToFill,
RenderTransform = new ScaleTransform(),
RenderTransformOrigin = new Point(0.5, 0.5)
};
// Add the UIElement to the adornment layer as an adornment
return this.AdornmentLayer.AddAdornment(AdornmentChangeReason.Other, image, new Point(), null, null);
}