旋转并缩放ImageBrush

时间:2014-11-06 09:59:15

标签: c# wpf xaml

我可以通过以下方式旋转图像:

  RotateTransform aRotateTransform = new RotateTransform();
  aRotateTransform.CenterX = 0.5;
  aRotateTransform.CenterY = 0.5;
  tateTransform.Angle = rotationAngle;

  ImageBrush bgbrush = new ImageBrush();
  bgbrush.RelativeTransform = aRotateTransform;

  ScaleTransform s = new ScaleTransform();
  s.ScaleX = -1; // how to set without overriding the rotation?
  ...

我如何另外缩放它?我尝试使用矩阵但没有成功。

4 个答案:

答案 0 :(得分:2)

你可以这样使用TransformGroup

TransformGroup tg = new Transformgroup();
tg.Children.Add(rotateTransform);
tg.Children.Add(scaleTransform);
bgbrush.RelativeTransform = tg;

答案 1 :(得分:1)

您可以使用CompositeTransform,它将翻译,旋转和缩放结合在一个矩阵中。

答案 2 :(得分:1)

只是为了完整。使用Matrix转换,您将得到预期的结果:

var transform = Matrix.Identity;
transform.RotateAt(rotationAngle, 0.5, 0.5);
transform.Scale(-1, 1);

bgbrush.RelativeTransform = new MatrixTransform(transform);

但是,我猜您实际上想要保持图像居中,因此您可以使用ScaleAt代替Scale

var transform = Matrix.Identity;
transform.RotateAt(rotationAngle, 0.5, 0.5);
transform.ScaleAt(-1, 1, 0.5, 0.5);

bgBrush.RelativeTransform = new MatrixTransform(transform);

答案 3 :(得分:0)

如果您只需要自行旋转Brush并稍后将其放置为矩形,而无需进行其他调整,那么这对我有用:

// Get the image from somewhere...
ib = new ImageBrush(bmp);

// Here I get it from a larger texture picture.
ib.Viewbox = this.GetVBRect(1728, 634);
ib.Transform = new RotateTransform()
{
    CenterX = 0.5,
    CenterY = 0.5,
    Angle = 180,
};
ib.TileMode = TileMode.Tile;