Silverlight:如何动态绘制图像?
s.jpg 通过 BuildAction==Resource 添加到解决方案中。
XAML
<UserControl x:Class=...>
<Canvas x:Name="LayoutRoot">
</Canvas>
</UserControl>
CS
Image Model = new Image();
Model.Source = new BitmapImage(new Uri("/s.jpg", UriKind.Relative));
LayoutRoot.SetLeft(Model, Coor.X);
LayoutRoot.SetTop(Model, Coor.Y);
Model.Width = 50;
Model.Height = 30;
LayoutRoot.Children.Add(Model);
这是我的问题:我的图像是什么?它没有出现在屏幕上,但如果我将 XAML 更改为
<UserControl x:Class=...>
<Canvas x:Name="LayoutRoot">
<Image Source="s.jpg"></Image>
</Canvas>
</UserControl>
您可以看到它,如果将 CS 更改为
Ellipse Model = new Ellipse();
Model.Fill = new SolidColorBrush(Colors.Gray);
Model.Stroke = new SolidColorBrush(Colors.Blue);
LayoutRoot.SetLeft(Model, Coor.X);
LayoutRoot.SetTop(Model, Coor.Y);
Model.Width = 50;
Model.Height = 30;
LayoutRoot.Children.Add(Model);
我也会看到该椭圆。那么问题出在哪里呢?
s.jpg added to solution with BuildAction==Resource.
XAML
<UserControl x:Class=...>
<Canvas x:Name="LayoutRoot">
</Canvas>
</UserControl>
CS
Image Model = new Image();
Model.Source = new BitmapImage(new Uri("/s.jpg", UriKind.Relative));
LayoutRoot.SetLeft(Model, Coor.X);
LayoutRoot.SetTop(Model, Coor.Y);
Model.Width = 50;
Model.Height = 30;
LayoutRoot.Children.Add(Model);
Here's my question: were is my image? It has't appeared on the screen, though if i change XAML to
<UserControl x:Class=...>
<Canvas x:Name="LayoutRoot">
<Image Source="s.jpg"></Image>
</Canvas>
</UserControl>
you can see it, also if change CS to
Ellipse Model = new Ellipse();
Model.Fill = new SolidColorBrush(Colors.Gray);
Model.Stroke = new SolidColorBrush(Colors.Blue);
LayoutRoot.SetLeft(Model, Coor.X);
LayoutRoot.SetTop(Model, Coor.Y);
Model.Width = 50;
Model.Height = 30;
LayoutRoot.Children.Add(Model);
i'll see that ellipse. so what the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
ImageSource imgSrc = new BitmapImage(new Uri("http://server/path/s.jpg", UriKind.RelativeOrAbsolute));
LayoutRoot.Children.Add(imgSrc);
LayoutRoot.UpdateLayout();
Try:
ImageSource imgSrc = new BitmapImage(new Uri("http://server/path/s.jpg", UriKind.RelativeOrAbsolute));
LayoutRoot.Children.Add(imgSrc);
LayoutRoot.UpdateLayout();