从新创建的项目开始,我在主页面添加一个按钮,并在点击处理程序中执行以下操作:
我创建了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);
}
答案 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对象 渲染。