背景画像を設定する
もっと簡単な方法があったのでメモ。
// Create source. BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.UriSource = new Uri("test.png", UriKind.Relative); bi.EndInit(); // Create the image element. Image simpleImage = new Image(); simpleImage.Width = bi.Width; simpleImage.Height = bi.Height; // Set the image source. simpleImage.Source = bi; this.Content = simpleImage; this.Background = Brushes.Red;
背景に画像を表示するのにこんなにコードを書くのか・・・。
public partial class TestWindow : Window { public TestWindow() { InitializeComponent(); ImageDrawing imageDrawing = new ImageDrawing(); imageDrawing.ImageSource = new BitmapImage(new Uri("test.png", UriKind.Relative)); imageDrawing.Rect = new Rect(0, 0, imageDrawing.ImageSource.Width, imageDrawing.ImageSource.Height); DrawingImage drawingImageSource = new DrawingImage(imageDrawing); // Freeze the DrawingImage for performance benefits. drawingImageSource.Freeze(); Image imageControl = new Image(); imageControl.Stretch = Stretch.None; imageControl.Source = drawingImageSource; imageControl.HorizontalAlignment = HorizontalAlignment.Left; imageControl.VerticalAlignment = VerticalAlignment.Top; this.Content = imageControl; this.Background = Brushes.Red; } }