wpf WPF在Canvas上显示Image的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12024952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems in diplaying Image on Canvas in WPF
提问by Tanuj Wadhwa
I have a Canvas on which I have dynamically added some shapes. I now want to add an Imageover the canvas.
我有一个画布,在上面动态添加了一些形状。我现在想Image在画布上添加一个。
Everything works fine, if I add Imageout of Canvasbut when I am trying to add the Imageon the Canvas, the image is not showing up. Maybe it is hidden behind the Shapes drawn on Canvas.
一切正常,如果我添加Image了,Canvas但是当我尝试在 上添加Image时Canvas,图像没有显示出来。也许它隐藏在绘制的形状后面Canvas。
My question is how can I bring the Imagein front of the Canvas.
我的问题是如何Image将Canvas.
This is how I am adding image :
这就是我添加图像的方式:
string strUri2 = String.Format(@"pack://application:,,,/MyFirstWPF;component/Images/tt.jpg");
image1.Source = new BitmapImage(new Uri(strUri2));
Note-> the shapes on canvas are added before adding the image.
注意-> 在添加图像之前添加画布上的形状。
回答by Colin Smith
You can do this 2 ways:
您可以通过 2 种方式执行此操作:
- change the Z-Index of the
Imageto bring it to the front of the Z-Order on theCanvas - use a container that allows the
Canvasto be overlaid with theImageelement (e.g aGrid)
- 更改的 Z-Index
Image使其位于 Z-Order 的前面Canvas - 使用允许
Canvas与Image元素重叠的容器(例如 aGrid)
If you use the Z-Index method with the Canvas then to do it properly you have to concern yourself with the Z-Index of other elements on the Canvas....because you have to choose a Z-Index value that is bigger than everything else.
如果您将 Z-Index 方法与 Canvas 一起使用,那么要正确执行此操作,您必须关注 Canvas 上其他元素的 Z-Index....因为您必须选择一个大于其他一切。
You could guess, and just pick a large Z-Index which you think will never be reached by the other elements, or you could scan through the children of the Canvas and find the highest Z-Index used, so that you can always set a higher one....so that you can ensure you "Bring to Front" the Image.
您可以猜测,只需选择一个您认为其他元素永远不会到达的大 Z-Index,或者您可以扫描 Canvas 的子项并找到使用的最高 Z-Index,以便您始终可以设置一个更高的一个......这样你就可以确保你“把图像放在前面”。
(this is probably the same as what you were using when you say "Out of Canvas")
(这可能与您说“Out of Canvas”时使用的相同)
There's an alternative way which means you don't have to explicitly bother with Z-Indexes, just use a Grid and specify the Canvas and Image as children...they will occupy the same cell, and so overlap each other...the Image being the last child will have the largest implied Z-Index.
还有一种替代方法,这意味着您不必明确地使用 Z-Indexes,只需使用 Grid 并将 Canvas 和 Image 指定为子项...它们将占据相同的单元格,因此彼此重叠...作为最后一个孩子的图像将具有最大的隐含 Z-Index。
<Grid>
<Canvas x:Name="mycanvas"/>
<Image x:Name="image1/>
</Grid>
回答by S3ddi9
simply use
简单地使用
myCanvas.Children.Insert(0,image1);
回答by Rohit Vats
You can set the z-indexof your image higher than other shapes to bring it on top of other shapes like this - Canvas.SetZIndex(image1, (int)5);
您可以将z-index图像的设置为高于其他形状,以将其置于其他形状之上 -Canvas.SetZIndex(image1, (int)5);
Refer to these links to know more about z-index - Controlling z-order, Z-Index of Canvas
请参阅这些链接以了解有关 z-index - Controlling z-order, Z-Index of Canvas 的更多信息

