为什么在之前渲染图像时不会出现?

时间:2011-11-06 21:50:24

标签: silverlight windows-phone-7 bitmap windows-phone-7.1

从新创建的项目开始,我在主页面添加一个按钮,并在点击处理程序中执行以下操作:

我创建了Image并将BitmapImage指定为Source。然后我将Image添加到我的LayoutRoot。我期望在单击按钮后在GUI中看到图像。

现在有了扭曲:我还希望将此Image呈现给WriteableBitmap。因此,我创建了这样一个位图,并调用其Render方法来渲染图像。

问题在于:当我注释掉Render来电时,我会立即看到Image出现在我的主页上。当我加入Render来电时,图片不会出现在第一个按钮上,而是出现在第二个上。为什么呢?

以下是代码:

    private void button1_Click(object sender, RoutedEventArgs e)
    {

        WriteableBitmap wbmp = new WriteableBitmap(62, 62);
        BitmapImage bmp = new BitmapImage(new Uri("ApplicationIcon.png", UriKind.Relative));
        Image img = new Image() { Width = 62, Height = 62, Source = bmp };

        wbmp.Render(img, null); // <------ this line makes the difference

        LayoutRoot.Children.Add(img);
    }

2 个答案:

答案 0 :(得分:0)

试试这个..

 private Image _img = new Image ();
    public Image img
    {
        get
        {
            return _img;
        }
    }
 public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String PropertyName)
    {
        if (null != PropertyChanged)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
    }
 private void button1_Click(object sender, RoutedEventArgs e)
{

    WriteableBitmap wbmp = new WriteableBitmap(62, 62);
    BitmapImage bmp = new BitmapImage(new Uri("ApplicationIcon.png", UriKind.Relative));
    Image img = new Image() { Width = 62, Height = 62, Source = bmp };

    wbmp.Render(img, null); // <------ this line makes the difference

    LayoutRoot.Children.Add(img);

      NotifyPropertyChanged("Image");
}

这应该有效..尝试一次......

答案 1 :(得分:0)

使用渲染有一些remarks(可能是其中一个让你的问题):

  

调用此方法后,必须调用Invalidate才能进行渲染   位图。

     

此方法支持不属于的UIElement对象   视觉树。您需要致电测量和安排   在调用之前,不在可视树中的UIElement对象   渲染。