windows-phone-7 – 在WP7上使用WriteableBitmap和RotateTransform渲染文本块

前端之家收集整理的这篇文章主要介绍了windows-phone-7 – 在WP7上使用WriteableBitmap和RotateTransform渲染文本块前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
似乎在Silverlight for Windows Phone的WriteableBitmap中存在一个非常烦人的错误.我有以下代码和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差别很大.有谁知道发生了什么事?

解决方法

我不完全理解发生了什么,但我相信控制尺寸是由于水平和垂直对齐而调整的,并且它会以某种方式导致您提到的问题.

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

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

猜你在找的Windows相关文章