2009-07-31 82 views
0

我正在WPF中为基于2D瓦片的游戏创建一个关卡编辑器。我试图找出加载瓦片集Image文件并将每个瓦片渲染到适当位置以重建地图的最佳方法。绘制一个BitmapSource的子区域

目前,我正在加载Image作为BitmapSource,并且我从Canvas类派生出用于显示地图的控件。我重写了OnRender方法,所以我可以得到一个DrawingContext。但是,DrawingContext.DrawImage似乎没有适当的超载,只能绘制图像的一个子目录;它看起来像我必须绘制整个图像。

如果我想将Image的子项划到Canvas上,应该如何使用?或者我应该使用Canvas以外的东西?

回答

0

这里是我会怎么做:

protected override void OnRender(DrawingContext dc) 
{ 
    BitmapImage source = new BitmapImage(); 
    source.BeginInit(); 
    source.UriSource = new Uri(@"pack://application:,,,/YourProject;component/YourImage.jpg"); 
    source.SourceRect = new Int32Rect(0, 0, 200, 200); 
    source.EndInit(); 

    dc.DrawImage(source, Rect.Parse("0, 0, 200, 200")); 
    base.OnRender(dc); 
} 

,这是否对你来说是BitmapImage.SourceRect属性。