除了单色,渐变画刷外,也可以使用图像作为模式(Pattern)画刷来填充图形。
下面的例子使用两个PNG图像来填充图形。
- private void Patterns()
- {
- TextureBrush brush1;
- TextureBrush brush2;
- TextureBrush brush3;
- AffineTransform matrix1 = new AffineTransform();
- AffineTransform matrix2 = new AffineTransform();
- BitmapImage img = new BitmapImage();
- img.CreateOptions = BitmapCreateOptions.None;
- string path = "/SilverlightGraphics2DDemo;component/brick.png";
- Stream s = Application.GetResourceStream
- (new Uri(path, UriKind.Relative)).Stream;
- img.SetSource(s);
- WriteableBitmap writeableBitmap = new WriteableBitmap(img);
- brush1 = new TextureBrush(writeableBitmap.Pixels,
- img.PixelWidth, img.PixelHeight);
- path = "/SilverlightGraphics2DDemo;component/bird.png";
- s = Application.GetResourceStream(new Uri(path, UriKind.Relative)).Stream;
- img.SetSource(s);
- writeableBitmap = new WriteableBitmap(img);
- brush2 = new TextureBrush(writeableBitmap.Pixels, img.PixelWidth,
- img.PixelHeight);
- brush3 = new TextureBrush(writeableBitmap.Pixels, img.PixelWidth,
- img.PixelHeight,127);
- matrix2.Translate(50, 50);
- graphics2D.Clear(Color.White);
- graphics2D.AffineTransform = (matrix1);
- graphics2D.FillRectangle(brush1, new Rectangle(20, 50, 100, 100));
- graphics2D.FillOval(brush2, 10, 10, 80, 80);
- graphics2D.AffineTransform = (matrix2);
- graphics2D.FillOval(brush3, 10, 10, 80, 80);
- }
这个例子所用的brick.png 和bird.png ,brush3是半透明画刷。