繁体   English   中英

向wpf添加图像

[英]Adding Image to wpf

我想将Image动态添加到wpf应用程序。

我尝试过的

            Image img = new Image();
            img.Height = 100;
            img.Width = 100;
            img.Visibility = Visibility.Visible;

            Thickness th = new Thickness(100, 100, 0, 0);
            img.Margin = th;

            string strUri2 = String.Format(@"pack://application:,,,/MyFirstWPF;component/Images/tt.jpg");
            img.Source = new BitmapImage(new Uri(strUri2));

我知道,除非将图像添加到内容中,否则图像不会显示。

this.Content=img;

但与此相关的是应用程序上现有的控件(形状)丢失了。

现在,我的问题是如何在不丢失应用程序现有控件​​的情况下将图像添加到内容。

当您要动态加载和显示图像时,您仍然需要考虑应用程序的布局。 我建议在XAML中(例如在Grid中)添加一个(最初是空的)Image控件,然后在代码中设置此控件的Source属性。

<Grid>
    ... other controls ...
    <Image Name="img" Grid.Row="..." Grid.Column="..."
           Width="100" Height="100" Margin="100,100,0,0"/>
</Grid>

在代码中设置Source代码:

var uri = new Uri("pack://application:,,,/MyFirstWPF;component/Images/tt.jpg");     
img.Source = new BitmapImage(uri);

默认情况下,窗口内容是网格,因此请尝试

(this.Content as Grid).Children.Add(img);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM