WPF: How to avoid an image coming out of the borders of the canvas?

I have a canvas as an image viewer. Its background is used to place the image, and I would like to add another image on top of it. So the scenario is as follows:

 <Canvas Name="VisorCanvas"  Height="514"  Width="720">
   <Canvas.Background>
   <ImageBrush />
   </Canvas.Background>
   <Image Name="foreground"  />
</Canvas>

I am dynamically inserting images into the code behind (C #).

The problem is that if the image is too large, then the image goes beyond the boundaries of the canvas. For example: I have an irrelevant background image, and I want to show a square inside the Canvas panel (over its background) as follows:

  • In any case, the square image will be resized.
  • if it is smaller than the canvas, I'll just show it.
  • ( ), , . - , "" ( )

? :

  • CroppedBitmap, .
  • opacityMask, ( ) , , .
  • "" RenderTargetBitmap , .

- .

+5
1

ClipToBounds="True" Canvas, , .

Image. Rectangle Fill, ImageBrush, Viewbox Viewport , , , . ( ViewportUnits Absolute, .)

+24

All Articles