貌似SilverLight中没有像XNA的Draw那样有丰富的重载,可以任意绘画,只能通过设置属性和特效。
现实的应用中往往又要在后台代码中实现而不是在XAML中设置:
int posX = 50;
int poxY = 100;
int spriteWidth = 50;
int spriteHeight = 100;
Uri uri2 = new Uri("/MusicSearch;component/Notes/note3.png", UriKind.Relative);
ImageSource noteListImageMap = new BitmapImage(uri2);
RectangleGeometry xRect = new RectangleGeometry();
xRect.Rect = new Rect(posX, poxY, spriteWidth, spriteHeight);
myImage.Source = noteListImageMap;
myImage.Stretch = Stretch.None;
myImage.Width = 350;
myImage.Height = 2900;
myImage.Clip = xRect;
TranslateTransform translateTransform = new TranslateTransform();
translateTransform.X = -posX;
translateTransform.Y = -poxY;
myImage.RenderTransform = translateTransform;
int poxY = 100;
int spriteWidth = 50;
int spriteHeight = 100;
Uri uri2 = new Uri("/MusicSearch;component/Notes/note3.png", UriKind.Relative);
ImageSource noteListImageMap = new BitmapImage(uri2);
RectangleGeometry xRect = new RectangleGeometry();
xRect.Rect = new Rect(posX, poxY, spriteWidth, spriteHeight);
myImage.Source = noteListImageMap;
myImage.Stretch = Stretch.None;
myImage.Width = 350;
myImage.Height = 2900;
myImage.Clip = xRect;
TranslateTransform translateTransform = new TranslateTransform();
translateTransform.X = -posX;
translateTransform.Y = -poxY;
myImage.RenderTransform = translateTransform;
首先载入图片,使用Clip属性来裁减图片的指定区域,再用TranslateTransform来实现偏移量,将裁出来的图片移动到原点,过程中反复实验发现图片控件的宽度和高度需要重新设置为sprite图片的高度和宽度,不然的话设置过小会让Clip裁减不到任何东西。