在运行时分割图像
我有一个像这样的图像:
我想要拥有它,这样我就可以将这些图像用于不同的按钮和其他部分,而不必自己拆分图像。我见过很多游戏和程序都可以做到这一点,而无需拆分它。我该怎么做?我正在使用 VB.net,因此任何 .net 示例都值得赞赏!
您可以在此处查看示例:
该图像在我玩的游戏中用作小地图,不同的部分在运行时。
I have an image such as this:
I want to have it so I can use these images for different buttons and other parts without having to split the image myself. I've seen lots of games and programs do this without needing to split it. How would I do this? I'm using VB.net so any .net examples are appreciated!
You can see an example here:
This image is used as a minimap in a game I play, the different pieces are cut at runtime.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看不出有什么办法可以自己分割图像。 某些东西必须从条带中提取这些单独的图像,以便您可以独立使用它们。
您显然对任何替代方法的建议(甚至询问)感到生气,所以我想我会跳过我的答案的这一部分......
在.NET中分割它的最简单方法是使用
图形
类。它包装了 GDI+ 并提供了几个有用的绘图相关函数。这里最重要的是DrawImage
,它提供了几种不同的重载,允许您指定要从中绘制的源图像内的区域的尺寸。通过改变这些坐标,您可以将条带中的每个单独图像绘制到新的位图
。一旦获得了位图,您就可以将其分配给控件、将其保存到磁盘,或者使用它执行任何您想要的操作。I can't see any way around splitting the image yourself. Something has to extract those individual images out of the strip so that you can use them independently.
You apparently take offense at the suggestion of (or even inquiry into) any alternative approaches, so I guess I'll skip that part of my answer...
The easiest way to split this up in .NET is using the
Graphics
class. which wraps GDI+ and provides several useful drawing-related functions. The most important one here isDrawImage
, which provides several different overloads that allow you to specify the dimensions of the region inside the source image to draw from. By varying these coordinates, you can extract each of the individual images in your strip by drawing them into a newBitmap
. And once you have theBitmap
, you can either assign that to a control, save it to disk, or do whatever else you want with it.以下是在 WPF 中执行此操作的方法:
Here's how to do it in WPF: