2011-08-17 28 views
2

如何使用自定义路径(在代码后面,而不是XAML中)在Silverlight中剪裁图像。 我有拼图一样的形状写在路径,并希望用它来剪辑任何图像。使用Silverlight中的路径剪裁图像

目前,它的工作原理是利用裁剪矩形,该代码是(C#):

private void UserControl_Loaded(object sender, RoutedEventArgs e) 
    { 
     int NUM_COLUMN = 8; 
     int NUM_ROW = 8; 
     double gridWidth = 60; 
     double gridHeight = 60; 
     string url = "Images/Sun.png"; 

     // C# 
     for (int i = 0; i < NUM_COLUMN; i++) 
     { 
      for (int j = 0; j < NUM_ROW; j++) 
      { 
       double offsetX = (double)i * gridWidth; 
       double offsetY = (double)j * gridHeight; 

       Image image = new Image(); 
       image.Source = new BitmapImage(new Uri(url, UriKind.Relative)); 

       // clip the image 

       RectangleGeometry r = new RectangleGeometry(); 


       r.Rect = new Rect(offsetX, offsetY, gridWidth, gridHeight); 
       image.Clip = r;     

       this.ClipCanvas.Children.Add(image); 
      } 
     } 
    } 

只有一个CanvasXAML称为ClipCanvas

+0

您可以提供您当前的代码或Xaml作为起点吗? –

+1

是的。查看编辑的问题。 – Arterius

回答

1

好吧,不理想。但是从代码隐藏

Image image = XamlReader.Load(@"<Image xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" Clip=""M 41.1,33.7 C 41.1,33.7 39.9,32.2 39.9,30.8 C 39.9,30.6 39.5,29.8 39.3,29.5 C 39.1,29.3 38.4,28.6 37.8,28.2 C 37.2,27.9 35,26.6 34.6,22.4 Z "" />") as Image; 
image.Source = new BitmapImage(new Uri("Desert.jpg", UriKind.Relative)); 

在XAML中夹设置工作,并建立使用XamlReader图像。尝试了其他方法,这是唯一有效的方法。