4

it seems that there is a really annoying bug in WriteableBitmap for Silverlight for Windows Phone. I have the following code and xaml:

public partial class MainPage : PhoneApplicationPage
{
    CompositeTransform rotate = new CompositeTransform();

    public MainPage()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        this.rotate.Rotation += 15;

        WriteableBitmap bmp = new WriteableBitmap(this.button, rotate);
        this.image.Source = bmp;

        Dispatcher.BeginInvoke(() => Debug.WriteLine("{0}, {1}", bmp.PixelWidth, bmp.PixelHeight));
    }
}

Here is the xaml:

<Grid>
    <Button VerticalAlignment="Top"
            HorizontalAlignment="Center"
            Content="This is a textblock inside a layout"
            x:Name="button"/>

    <Image VerticalAlignment="Center"
           HorizontalAlignment="Center"
           x:Name="image"/>

    <Button VerticalAlignment="Bottom"
            Content="Rotate"
            Click="Button_Click"/>
</Grid>

When you click the bottom button, the top button is rendered with the writeable bitmap using the composite transform. After every render, the resulting image of the top button is larger than the previous one. Also, the PixelWith and PixelHeight property values of the writeable bitmap differ wildly from the RenderSize of the Image object. Does anyone have any idea what is going on?

Viktor
  • 39
  • 2

1 Answers1

1

I don't fully understand what's going on, but I believe the controls size are adjusted because of the horizontal and vertical alignment, and somehow it causes the issue you mentioned.

You can bypass it by setting the Stretch property of the Image control to None. This way, the displayed picture will always keep its original size, no matter the size of the image control.

        <Image  VerticalAlignment="Center"
                HorizontalAlignment="Center"
                Stretch="None"
                x:Name="image"/>
Kevin Gosse
  • 38,392
  • 3
  • 78
  • 94
  • I don't think it is caused by the alignments. Given that they are both Center the Image should be given its desired size, and it should be just as big as the image source suggests. But this is the minor issue, my main problem is the writeable bitmap and its resulting PixelWidth and PixelHeight values, they seem incorrect. I am trying to use them to find the bounding box of the transformed element but it seems that it does not work as I expected. – Viktor Aug 27 '12 at 08:48