Calculating the center of rotation after translation

后端 未结 1 1886
[愿得一人]
[愿得一人] 2021-01-22 11:04

I need to be able to rotate an image around a given point so that what ever part of the image appears in the center of my container is the center of rotation.

To calcula

相关标签:
1条回答
  • 2021-01-22 11:19

    If you are working with GDI+ then use the following:

    double ImWidth = (double)Im.Width;
    double ImHeight = (double)Im.Height;
    double XTrans = -(ImWidth * X);
    double YTrans = -(ImHeight * Y);
    
    g.TranslateTransform((float)XTrans, (float)YTrans);    
    g.TranslateTransform((float)(ImWidth / 2.0 - XTrans), (float)(ImHeight / 2.0 - YTrans));
    g.RotateTransform((float)Angle);
    g.TranslateTransform(-((float)(ImWidth / 2.0 - XTrans)), -((float)(ImHeight / 2.0 - YTrans)));
    

    If you are working with WPF graphic objects, use the following transform group:

    TransformGroup TC = new TransformGroup();
    RotateTransform RT = new RotateTransform(Angle);
    RT.CenterX = Im.Width / 2.0;
    RT.CenterY = Im.Height / 2.0;
    TranslateTransform TT = new TranslateTransform(-X * Im.PixelWidth, -Y * Im.PixelHeight);
    TC.Children.Add(TT);
    TC.Children.Add(RT);
    

    X & Y are the percent values you want to translate the image in (if the image is 1000 pixels and X is 0.1 then the image will be translated 100 pixels). This is how I needed the function to work but you can easily change it otherwise.

    0 讨论(0)
提交回复
热议问题