简体   繁体   中英

How to WPF draw rectangle on top of image view dragged onto canvas

I have images that I can drag around on screen. On these images, I want to be able to draw rectangles in predefined spots on each image (think like drag handles, although they will not be used that way). Whenever the image gets dragged somewhere new, I would like these rectangles to be drawn in the same spot on the image. This includes if the image is rotated too.

I have tried a number of different things but cannot get anything to work so I am hesitant to describe any of them. This was easy to achieve with Winforms but since I can't access a Paint method and it seems that the only way to set coordinates of a rectangle is Canvas.SetLeft(image, x), Canvas.SetTop(image, y) and these rectangles should be a child of the image, I'm finding this very un-intuitive.

How would I go about doing this?

You have to use Adorners . There are many good tutorials out there :

WPF: Thinking outside the box with the adorner

How to: Implement an Adorner

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM