在运行时分割图像

发布于 2024-11-03 10:56:19 字数 297 浏览 0 评论 0原文

我有一个像这样的图像:

我想要拥有它,这样我就可以将这些图像用于不同的按钮和其他部分,而不必自己拆分图像。我见过很多游戏和程序都可以做到这一点,而无需拆分它。我该怎么做?我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷…雨湿花 2024-11-10 10:56:19

我看不出有什么办法可以自己分割图像。 某些东西必须从条带中提取这些单独的图像,以便您可以独立使用它们。

您显然对任何替代方法的建议(甚至询问)感到生气,所以我想我会跳过我的答案的这一部分......

在.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 is DrawImage, 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 new Bitmap. And once you have the Bitmap, you can either assign that to a control, save it to disk, or do whatever else you want with it.

老子叫无熙 2024-11-10 10:56:19

以下是在 WPF 中执行此操作的方法:

using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;

class Program
{
    static void Main(string[] args)
    {
        var file = @"Imagestrip1600x16.png";
        var output = @"IconsFolder";
        using (Stream imageStreamSource = new FileStream(
            file, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            PngBitmapDecoder decoder = new PngBitmapDecoder(
                imageStreamSource,
                BitmapCreateOptions.PreservePixelFormat,
                BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[0];

            for (int i = 0; i < bitmapSource.PixelWidth / 16; i++)
            {
                CroppedBitmap croppedBitmap = new CroppedBitmap(
                    bitmapSource,
                    new Int32Rect(i * 16, 0, 16, 16));

                PngBitmapEncoder encoder = new PngBitmapEncoder();
                var frame = BitmapFrame.Create(croppedBitmap);
                encoder.Frames.Add(frame);
                var fileName = Path.Combine(output, i.ToString() + ".png");
                using (var stream = new FileStream(fileName, FileMode.Create))
                {
                    encoder.Save(stream);
                }
            }
        }
    }
}

Here's how to do it in WPF:

using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;

class Program
{
    static void Main(string[] args)
    {
        var file = @"Imagestrip1600x16.png";
        var output = @"IconsFolder";
        using (Stream imageStreamSource = new FileStream(
            file, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            PngBitmapDecoder decoder = new PngBitmapDecoder(
                imageStreamSource,
                BitmapCreateOptions.PreservePixelFormat,
                BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[0];

            for (int i = 0; i < bitmapSource.PixelWidth / 16; i++)
            {
                CroppedBitmap croppedBitmap = new CroppedBitmap(
                    bitmapSource,
                    new Int32Rect(i * 16, 0, 16, 16));

                PngBitmapEncoder encoder = new PngBitmapEncoder();
                var frame = BitmapFrame.Create(croppedBitmap);
                encoder.Frames.Add(frame);
                var fileName = Path.Combine(output, i.ToString() + ".png");
                using (var stream = new FileStream(fileName, FileMode.Create))
                {
                    encoder.Save(stream);
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文