wpf 如何给TextBlock设置背景图片
TextBlock box = new TextBlock();
ImageBrush brush = new ImageBrush();
brush.ImageSource = BaseWindow.ImageShift("../Image/Bg.png");
box.Background = brush;
这样不行?
[解决办法]
使用如下方法将图片路径转成画刷
/// <summary> /// 根据图片路径生成图片画刷 /// </summary> /// <param name="uri">图片路径</param> /// <returns>图片画刷</returns> internal static ImageBrush GenerateImageBurshByUri(string uri) { if (string.IsNullOrEmpty(uri)) throw new ArgumentNullException(); BitmapImage bm = new BitmapImage(); bm.BeginInit(); try { bm.UriSource = new Uri(uri); bm.EndInit(); } catch (System.IO.FileNotFoundException) { return null; } ImageBrush im = new ImageBrush(); im.ImageSource = bm; return im; }
[解决办法]
哪需要那么麻烦啊……
textBlock1.Background = new ImageBrush(new BitmapImage(new Uri(@"../Image/Bg.png", UriKind.RelativeOrAbsolute)));