Silverlight 引路蜂二维图形库示例:材质画刷

jerry Silverligth 2015年11月26日 收藏

除了单色,渐变画刷外,也可以使用图像作为模式(Pattern)画刷来填充图形。

下面的例子使用两个PNG图像来填充图形。

  1. private void Patterns()
  2. {
  3. TextureBrush brush1;
  4. TextureBrush brush2;
  5. TextureBrush brush3;
  6.  
  7. AffineTransform matrix1 = new AffineTransform();
  8. AffineTransform matrix2 = new AffineTransform();
  9.  
  10. BitmapImage img = new BitmapImage();
  11. img.CreateOptions = BitmapCreateOptions.None;
  12. string path = "/SilverlightGraphics2DDemo;component/brick.png";
  13. Stream s = Application.GetResourceStream
  14. (new Uri(path, UriKind.Relative)).Stream;
  15. img.SetSource(s);
  16. WriteableBitmap writeableBitmap = new WriteableBitmap(img);
  17. brush1 = new TextureBrush(writeableBitmap.Pixels,
  18. img.PixelWidth, img.PixelHeight);
  19. path = "/SilverlightGraphics2DDemo;component/bird.png";
  20. s = Application.GetResourceStream(new Uri(path, UriKind.Relative)).Stream;
  21. img.SetSource(s);
  22. writeableBitmap = new WriteableBitmap(img);
  23. brush2 = new TextureBrush(writeableBitmap.Pixels, img.PixelWidth,
  24. img.PixelHeight);
  25. brush3 = new TextureBrush(writeableBitmap.Pixels, img.PixelWidth,
  26. img.PixelHeight,127);
  27. matrix2.Translate(50, 50);
  28. graphics2D.Clear(Color.White);
  29. graphics2D.AffineTransform = (matrix1);
  30. graphics2D.FillRectangle(brush1, new Rectangle(20, 50, 100, 100));
  31. graphics2D.FillOval(brush2, 10, 10, 80, 80);
  32. graphics2D.AffineTransform = (matrix2);
  33. graphics2D.FillOval(brush3, 10, 10, 80, 80);
  34.  
  35. }

这个例子所用的brick.png 和bird.png ,brush3是半透明画刷。