2011-08-17 75 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圖像。嘗試了其他方法,這是唯一有效的方法。