I am drawing at dc (DrawingContext) of a Grid like this:
dc.DrawImage(_imageSource, new Rect(x, y, width, height);
But, what if I wanted to rotate the image I am drawing on drawing context?
I know I can push effect to drawing context, but, it will affect all the drawings, while I want to rotate only drawing of _imageSource.
Any idea?
You could push a RotateTransform before drawing the image, and immediately pop the transform after drawing.
Alternatively you could create an ImageDrawing, add it to a DrawingGroup (which has a
Transformproperty), and then draw the DrawingGroup:All the DrawingContext.Draw… methods actually create and draw the appropriate Drawing objects.
Instead of continuously redrawing everything in an overridden OnRender() method you could also do the following:
Create a DrawingVisual as a container for your drawings. See Using DrawingVisual Objects about how to do that.
Create a DrawingGroup with an ImageDrawing as shown above and draw the DrawingGroup once into the DrawingContext provided by DrawingVisual.RenderOpen.
Use a MatrixTransform or a TransformGroup for the complete image transformation, i.e. translation and rotation. Set the DrawingGroup.Transform property to this transform (instead of RotateTransform only as shown above).
When an image’s position or rotation angle changes, only change the related transform. Redrawing is not necessary.