程序化实现Grid的ImageBrush

5

我尝试应用图像但没有看到任何变化...

我缺少什么?谢谢!!

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(@"pack://application:,,,/Images/bg1.jpg", UriKind.RelativeOrAbsolute);
bi.EndInit();
ImageBrush ib = new ImageBrush();
ib.TileMode = TileMode.Tile;
ib.ImageSource = bi;
ib.Stretch = Stretch.None;
RootGrid.Background = ib;
1个回答

9
请尝试以下内容:
var ib = new ImageBrush {
  ImageSource =
    new BitmapImage(
      new Uri(@"Images\bg1.jpg", UriKind.Relative)
    )
};

RootGrid.Background = ib;

另外,这是显而易见的,但请确保图像实际上位于正确的路径并设置为项目中的内容。


抱歉,我有一个嵌入式图像,你的代码给了我一个错误。 - NoWar
什么是错误?什么是嵌入式图像?您将其设置为项目中的<Content>还是<Resource> - justin.m.chase
我有一个<Resource>,它是DLL UserControl项目。 - NoWar
2
好的,我已经修复了。ImageBrush ib = new ImageBrush(new BitmapImage(new Uri(@"pack://application:,,,/MyDllName;Component/Images/bg1.jpg"))); - NoWar
啊,好的。那个包URI语法有时很难调试。 - justin.m.chase
1
@AcademyofProgrammer 提出的解决方案完美地解决了问题! - Gláucio Leonardo Sant'ana

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接