如何将一个图像叠加到另一个图像上?
问题描述:
我想显示由两个图片组成的图片。
I would like to display an image composed of two images.
我想要图片 rectangle.png 在顶部上显示图片 sticker.png ,其左侧角位于像素10,10处。
I want image rectangle.png to show with image sticker.png on top of it with its left-hand corner at pixel 10, 10.
这是我的目标,但如何合并图像?
Image image = new Image();
image.Source = new BitmapImage(new Uri(@"c:\test\rectangle.png"));
image.Stretch = Stretch.None;
image.HorizontalAlignment = HorizontalAlignment.Left;
Image imageSticker = new Image();
imageSticker.Source = new BitmapImage(new Uri(@"c:\test\sticker.png"));
image.OverlayImage(imageSticker, 10, 10); //how to do this?
TheContent.Content = image;
答
您需要一个Panel才能将两个Image控件添加到。网格或画布将允许这样,但我会使用网格,因为它将约束图像控件(从而适当地拉伸或缩小它们)。
You need a Panel to add both Image controls to. A Grid or Canvas will allow this, but I would go with the Grid as it will constrain the Image controls (thereby stretching or shrinking them as appropriate).
Image image = new Image();
image.Source = new BitmapImage(new Uri(@"c:\test\rectangle.png"));
image.Stretch = Stretch.None;
image.HorizontalAlignment = HorizontalAlignment.Left;
Image imageSticker = new Image();
imageSticker.Source = new BitmapImage(new Uri(@"c:\test\sticker.png"));
imageStiker.Margin = new Thickness(10, 10, 0, 0);
Grid grid = new Grid();
grid.Children.Add(image);
grid.Children.Add(imageSticker);
TheContent.Content = grid;