在WP7上使用WriteableBitmap和RotateTransform渲染文字块

4

看起来,在Windows Phone的Silverlight中存在一个非常让人烦恼的WriteableBitmap bug。以下是我的代码和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));
    }
}

这是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>

当您点击底部按钮时,使用合成变换将可写位图渲染为顶部按钮。每次渲染后,顶部按钮的结果图像比以前的大。此外,可写位图的PixelWith和PixelHeight属性值与Image对象的RenderSize值相差很大。有人知道发生了什么吗?

1个回答

1

我不完全理解发生了什么,但我相信控件大小是由于水平和垂直对齐而调整的,而且某种方式导致了你提到的问题。

您可以通过将Image控件的Stretch属性设置为None来绕过它。这样,显示的图片将始终保持其原始大小,无论图像控件的大小如何。

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

我认为这不是由对齐方式引起的。考虑到它们都是居中的,图像应该被赋予其所需的大小,并且应该与图像源一样大。但这只是小问题,我的主要问题是可写位图及其生成的PixelWidth和PixelHeight值,它们似乎不正确。我试图使用它们来找到转换元素的边界框,但似乎它并不像我预期的那样工作。 - Viktor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接