我创建了一个新的WPF控件,添加了一个Rectangle,并且它的工作正常,它的绘制方式应该是这样。但我不能用真实的图像绘制矩形。用图像填充WPF矩形
BitmapImage bi = GetImage();
ImageBrush imgBrush= new ImageBrush(bi);
this.rectangle.Fill = imgBrush;
但这段代码只是使矩形透明,除了笔画。
这是GetImage()
方法:
BitmapImage bi;
using (MemoryStream ms = new MemoryStream())
{
bi = new BitmapImage();
bi.CacheOption = BitmapCacheOption.OnLoad;
texture.SaveAsPng(ms, texture.Width, texture.Height);
ms.Seek(0, SeekOrigin.Begin);
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
ms.Close();
}
return bi;
texture
是Texture2D
类,即此代码之前作出。
如果我在此返回Bitmap
insted BitmapImage
,然后保存该Bitmap
图片绘制正确。
谢谢您的帮助
任何人都知道为什么它不工作? – user1806687 2013-02-28 15:16:26