Search code examples
c#silverlighttransformcenteringwriteablebitmap

WriteableBitmap Transform to Center?


I need to put an user control (a TextBlock in this case) in the horizontal center of a WriteableBitmap, here is the code that i come with so far:

textblock1.RenderTransformOrigin = new Point(0.5, 0.5);
wp.Render(textblock1, new TranslateTransform() {Y = topMargin, X = imgWidth / 2});

but it appears that the pivot point of the textblock is still at the left edge of the control, where did i do wrong?


Solution

  • You may be transforming by the center, but the TranslateTransform moves the center the same distance as any other point. It's like grabbing a piece of paper and moving it an inch left. It doesn't matter where you grab it.

    Try this:

    wp.Render(textBlock1, new TranslateTransform() {Y = topMargin - textBlock1.Height / 2, X = imgWidth / 2 - textBlock1.Width / 2});
    

    You can forget about RenderTransformOrigin unless you are using rotates/scales/matrices etc.