如何在WPF中叠加图像? [英] How to overlay images in WPF?
问题描述
我有两个需要重叠的图像。
I have two images which I need to overlap.
例如,我需要在实心矩形上方放置一个实心圆。因此,我可以看到整个圆,但只能看到矩形的角。我不希望圈子是透明的。原始的 circle.png
没有矩形边框。它的边界仅沿着圆的边界。因此,理想情况下,整个圆不应与矩形重叠。
For ex- I need a solid circle on top of a solid rectangle. So, I can see the entire circle but only the corners of the rectangle. I do not want the circle to be transparent. The original circle.png
does NOT have a rectangular border. Its border is along the boundaries of the circle only. Thus, ideally, the entire circle should not overlap the rectangle.
这是显示图像的代码:
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(imgpath, UriKind.Absolute);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
ImageBrush ib = new ImageBrush(src);
ib.Viewbox = new Rect(UVRectangle.X / src.PixelWidth, UVRectangle.Y / src.PixelHeight, UVRectangle.Width / src.PixelWidth, UVRectangle.Height / src.PixelHeight);
gr.Background = ib; //gr is of type Grid
但是,如果我两次调用此函数,第二个图像(圆圈)完全覆盖了矩形,因此,我最终看到的是画布的背景,而不是矩形的角。
However, if I call this function twice, the second image (circle) completely covers the rectangle and thus, I end up seeing the background of the canvas instead of the corners of the rectangle.
我想我需要合并两个图像,然后绘制它们。我应该如何去做?
I think I need to merge the two images and then paint them. How should I go about doing this?
推荐答案
您用第二个调用覆盖了Background属性。
如果以编程方式将两个图像添加到网格中,它将起作用。更改:
You override the property Background with your second call. If you add two Images to your Grid programmatically it would work. Change:
gr.Background = ib;
收件人:
Image image = new Image();
image.Source = ib.ImageSource;
gr.Children.Add(image);
这篇关于如何在WPF中叠加图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!