locked
Scaling an object via C# RRS feed

  • Question

  • Greetings All,

     

    I'm trying to scale a 'rectangle' via C#.  I know how to rotate it ... "MyRectangle.RenderTransform = new RotateTransform (180);"

    ... and assume scaling it is something similar e.g. "MyRectangle.RenderTransform = new ScaleTransform.ScaleX (2.0);"

     

    ... but this is obviously wrong (because it doesn't work :S ), could someone point me in the right direction;  Similarly, is there some convenient lookup table 'how to control these object values' (because they don't appear to follow a common format and are not all as straightforward as MyRectangle.Width = 50; etc)

     

    Many Thanks,

    Regards

    -RJ-

    (ExpressionBlend 3, WPF, C#)

     

    Friday, June 11, 2010 12:58 PM

Answers

  • ScaleX is not a property of the rect.. you need to use the render transform like this.

     

    ScaleTransform scale = new ScaleTransform();
    scale.ScaleX = 2.0;
    rect.RenderTransform = scale;

    • Proposed as answer by devdmans Friday, June 11, 2010 3:10 PM
    • Marked as answer by dt01rrj Friday, June 11, 2010 3:27 PM
    Friday, June 11, 2010 3:09 PM
  • Your original post seemed to have the correct method:

     

    this.MyRect.RenderTransform = new ScaleTransform(0.5, 0.5);

     

    This will decrease the rectangle to 50% of its original size (not its current size).  A render transform only affects the rendering so if you replace it with a new one, the previous one will disappear.

    • Proposed as answer by Chuck HaysModerator Friday, June 11, 2010 3:22 PM
    • Marked as answer by dt01rrj Friday, June 11, 2010 3:49 PM
    Friday, June 11, 2010 3:21 PM
    Moderator

All replies

  • This should get you up to speed rather quickly. Explanation and examples:

    http://msdn.microsoft.com/en-us/library/system.windows.media.scaletransform%28VS.95%29.aspx

     

    Also, if you scaled by 2.0 like in your example above, you would have scaled the object up 100% (x2). Think of it as 1 = current object size (x/y) and 2.0 as the multiplier.  So 1 x 1.5 would be scaled up by 50%.

    • Proposed as answer by devdmans Friday, June 11, 2010 3:10 PM
    Friday, June 11, 2010 1:33 PM
  • Greetings devdmans,

     

    Many thanks for your reply, I'm still having a little difficulty ...

     

    ... assuming I want to scale my rectangle by 200% then this should work (...with 'System.Windows.Media' referenced in my wpf doc):

    "MyRectangle.ScaleX = MyRectangl e.ScaleX * 1.25;" 

    But this throws up an error on build stating that " 'System.Windows.Shapes.Rectangle' does not contain definition for 'ScaleX' and no extension 'ScaleX' " .... Any thoughts?

    Many Thanks,

    -RJ-

    ( ExpressionBlend 3, WPF, C# )

    • Edited by dt01rrj Friday, June 11, 2010 2:16 PM error
    Friday, June 11, 2010 2:10 PM
  • ScaleX is not a property of the rect.. you need to use the render transform like this.

     

    ScaleTransform scale = new ScaleTransform();
    scale.ScaleX = 2.0;
    rect.RenderTransform = scale;

    • Proposed as answer by devdmans Friday, June 11, 2010 3:10 PM
    • Marked as answer by dt01rrj Friday, June 11, 2010 3:27 PM
    Friday, June 11, 2010 3:09 PM
  • Your original post seemed to have the correct method:

     

    this.MyRect.RenderTransform = new ScaleTransform(0.5, 0.5);

     

    This will decrease the rectangle to 50% of its original size (not its current size).  A render transform only affects the rendering so if you replace it with a new one, the previous one will disappear.

    • Proposed as answer by Chuck HaysModerator Friday, June 11, 2010 3:22 PM
    • Marked as answer by dt01rrj Friday, June 11, 2010 3:49 PM
    Friday, June 11, 2010 3:21 PM
    Moderator
  • Greetings devdmans,

     

    Many thanks for the solution.  I don't understand the format or why it works, but it does!  Much appreciated.

     

    Best Regards,

    -RJ-

    Friday, June 11, 2010 3:27 PM
  • Hi Chuck,

     

    Much neater and I can understand it... Many thanks.

     

    Best Regards,

    -RJ-

    Friday, June 11, 2010 3:49 PM