加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

WPF的ImageBrush类及窗体或控件的背景图像设置的C#代码实现方法

(2014-04-23 02:08:51)
ImageBrush 类

ImageBrush 是一种将自身内容定义为图像的 TileBrush,图像通过它的 ImageSource 属性指定。您可以控制图像的拉伸、对齐和平铺方式,因此可以生成图案和其他效果。下面的图像演示使用 ImageBrush 可以实现的一些效果。

ImageBrush 可以绘制形状、控件、文本等

http://i.msdn.microsoft.com/dynimg/IC131487.gif
示例

此示例演示如何使用 ImageBrush 类来绘制带有图像的区域。ImageBrush 显示由其 ImageSource 属性指定的单个图像。

下面的示例通过使用 ImageBrush 绘制按钮的 Background

原始代码参见如下网址:

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 PaintingWithImagesExample : Page

    {



        public PaintingWithImagesExample()

        {

            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文件中的窗体背景图像的设置代码

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有