我尝试这样做,但在将其设置为网格背景之前,我不知道如何设置ImageBrush
的高度和宽度。
BitmapImage bmp= new BitmapImage(new Uri(imagePath, UriKind.Relative));
int height = bmp.DecodePixelHeight;
int width = bmp.DecodePixelWidth;
ImageBrush imgBrush = new ImageBrush();
imgBrush.ImageSource = bmp;
MainGrid.Background = imgBrush // MainGrid is the name of the Grid
如果您只想设置 imageBrush 以正确填充腰围,请将 imageBrush 的拉伸属性设置为均匀(保持比例但可能会留下空白点)或均匀填充(保持比例,不留下空白点,但整个图片可能不适合网格)。
否则,这里有更复杂的代码来设置imageBrush的确切像素宽度和高度。
http://msdn.microsoft.com/en-us/library/ms742114.aspx