WPF的ImageBrush类及窗体或控件的背景图像设置的C#代码实现方法
ImageBrush 类
http://i.msdn.microsoft.com/dynimg/IC131487.gif
ImageBrush
ImageBrush 可以绘制形状、控件、文本等
http://i.msdn.microsoft.com/dynimg/IC131487.gif
示例
此示例演示如何使用
下面的示例通过使用
原始代码参见如下网址:
http://msdn.microsoft.com/zh-cn/library/system.windows.media.imagebrush(v=vs.90).aspx
C#代码
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;
namespace Microsoft.Samples.Graphics.UsingImageBrush
{
public class PaintingWithImagesExampl e : Page
{
public PaintingWithImagesExampl e()
{
Background = Brushes.White;
StackPanel mainPanel = new StackPanel();
mainPanel.Margin = new Thickness(20.0);
// Create a button.
Button berriesButton = new Button();
berriesButton.Foreground = Brushes.White;
berriesButton.FontWeight = FontWeights.Bold;
FontSizeConverter sizeConverter = new FontSizeConverter();
berriesButton.FontSize = (Double)sizeConverter.ConvertFromString("16pt");
berriesButton.FontFamily = new FontFamily("Verdana");
berriesButton.Content = "Berries";
berriesButton.Padding = new Thickness(20.0);
berriesButton.HorizontalAlignment = HorizontalAlignment.Left;
// Create an ImageBrush.
ImageBrush berriesBrush = new ImageBrush();
berriesBrush.ImageSource =
new BitmapImage(
new Uri(@"sampleImages\berries.jpg", UriKind.Relative)
);
// Use the brush to paint the button's background.
berriesButton.Background = berriesBrush;
mainPanel.Children.Add(berriesButton);
this.Content = mainPanel;
}
}
}
XAML代码 参看msdn链接
http://msdn.microsoft.com/zh-cn/library/system.windows.media. =====================
书上BasicClock项目(P538-P539).cs文件代码的修改
在构造函数中
InitializeComponent();
代码后,增加如下设置窗体背景图像的代码
ImageBrush berriesBrush = new ImageBrush();
berriesBrush.ImageSource = new BitmapImage(new Uri(@"images\circle.png", UriKind.Relative));
this.Background = berriesBrush;
=============
同时删除xaml文件中的窗体背景图像的设置代码

加载中…