1

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

目前它通过使用矩形进行裁剪来工作,代码是(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);
            }
        }
    }

XAMLCanvas中只有一个名为.ClipCanvas

4

1 回答 1

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 创建图像。尝试了其他方法,这是唯一有效的方法。

于 2011-08-17T11:01:52.510 回答