Windows Phone 控件的位图



是否可以在Windows Phone中获取特定控件的位图?
在WindowsForm应用程序中,使用DrawToBitmap方法可以实现这一点,但在Windows Phone中没有这样的方法。
我该怎么办?

可以使用WriteableBitmap类。

假设您有两个控件,一个按钮和一个图像:

<StackPanel>
    <Button x:Name="Button1" Content="Test" Click="Button1_Click" />
    <Image x:Name="Image1" />
</StackPanel>

并且您想从按钮生成位图,并将其分配给Image1。然后,只需使用需要 UIElement 的可写位图构造函数,并将位图分配给图像控件:

private void Button1_Click(object sender, RoutedEventArgs e)
{
    // Creates a bitmap with a visual representation of the button
    var bitmap = new WriteableBitmap(this.Button1, null);
    // Assigns the bitmap to the image control
    this.Image1.Source = bitmap;
}

最新更新