我正在 WPF 中为基于 2D 图块的游戏创建一个关卡编辑器。 我正在尝试找出加载tileset Image
文件并在适当的位置渲染每个图块以重建地图的最佳方法。
目前,我将 Image
作为 BitmapSource
加载,并从 Canvas
类派生用于显示地图的控件。 我将重写 OnRender
方法,以便获得 DrawingContext
。 但是,DrawingContext.DrawImage 似乎没有适当的重载来仅绘制图像的子矩形; 看起来我必须画出整个图像。
如果我想将 Image
的子矩形绘制到 Canvas
上,我应该使用什么? 或者我应该使用 Canvas
以外的东西?
I'm creating a level editor in WPF for a 2D tile-based game. I'm trying to figure out the best way to load the tileset Image
file and render each tile in the appropriate location to reconstruct the map.
Currently, I'm loading the Image
as a BitmapSource
, and I'm deriving from the Canvas
class for the control that displays the map. I'm overriding the OnRender
method so I can get a DrawingContext
. However, DrawingContext.DrawImage
doesn't appear to have an appropriate overload that draws only a subrect of an image; it looks like I have to draw the entire image.
What should I use if I want to draw subrects of an Image
onto a Canvas
? Or should I be using something other than a Canvas
?
发布评论
评论(1)
我将这样做:
为您执行此操作的属性是 BitmapImage.SourceRect。
Here is how I would do it:
The property that does this for you is BitmapImage.SourceRect.