简体   繁体   English

如何在 XNA 中调整和保存 Texture2D 的大小?

[英]How to resize and save a Texture2D in XNA?

In a level editor I've made for my XNA game (editor is also in XNA) I'm allowing to scale Texture2D objects.在我为 XNA 游戏制作的关卡编辑器中(编辑器也在 XNA 中),我允许缩放Texture2D对象。

When the user tries to save the level, I'd like to actually resize the image file on disk so that it doesn't require scaling in the game.当用户尝试保存关卡时,我想实际调整磁盘上的图像文件大小,以便它不需要在游戏中缩放。

Is there an easy way to create an image file (PNG preferred) from a scaled Texture2D object?有没有一种简单的方法可以从缩放的 Texture2D object 创建图像文件(首选 PNG)?

You can scale your textures by rendering to a render target at the size you want and then saving the render target texture.您可以通过以所需大小渲染到渲染目标然后保存渲染目标纹理来缩放纹理。

This simple example shows how you could do that.这个简单的例子展示了如何做到这一点。 Ignore the setup of the GraphicsDevice, that's just to make a small self-contained example.忽略GraphicsDevice的设置,这只是做一个自包含的小例子。 The interesting bit is creating the render target and drawing the scaled texture.有趣的是创建渲染目标并绘制缩放的纹理。 You should reuse the render targets where you can (all images of the same size can reuse a render target).您应该尽可能重用渲染目标(所有相同大小的图像都可以重用渲染目标)。

using System;
using System.Runtime.InteropServices;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

class Program
{
    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern IntPtr GetConsoleWindow();

    static void Main(string[] args)
    {
        string sourceImagePath = args[0];
        string destinationImagePath = args[1];
        int desiredWidth = int.Parse(args[2]);
        int desiredHeight = int.Parse(args[3]);

        GraphicsDevice graphicsDevice = new GraphicsDevice(
            GraphicsAdapter.DefaultAdapter,
            DeviceType.Hardware,
            GetConsoleWindow(),
            new PresentationParameters());
        SpriteBatch batch = new SpriteBatch(graphicsDevice);

        Texture2D sourceImage = Texture2D.FromFile(
            graphicsDevice, sourceImagePath);

        RenderTarget2D renderTarget = new RenderTarget2D(
            graphicsDevice, 
            desiredWidth, desiredHeight, 1, 
            SurfaceFormat.Color);

        Rectangle destinationRectangle = new Rectangle(
            0, 0, desiredWidth, desiredHeight);

        graphicsDevice.SetRenderTarget(0, renderTarget);

        batch.Begin();
        batch.Draw(sourceImage, destinationRectangle, Color.White);
        batch.End();

        graphicsDevice.SetRenderTarget(0, null);

        Texture2D scaledImage = renderTarget.GetTexture();
        scaledImage.Save(destinationImagePath, ImageFileFormat.Png);
    }
}

I have code like this that makes a larger/smaller copy of a texture but I get errors after a while.我有这样的代码,可以制作更大/更小的纹理副本,但一段时间后出现错误。 After 30 resizes then I get the exception, not the first time.在 30 次调整大小之后,我得到了异常,而不是第一次。

Seems to be a rendertarget problem but I am not sure.似乎是一个渲染目标问题,但我不确定。 Wonder if you will get these exceptions too if you run this code a lot too?想知道如果您也经常运行此代码,您是否也会遇到这些异常?

Have you tried Texture2D.Save() ?你试过Texture2D.Save()吗?

Scaling images generally does not incur all that much overhead, plus, you wouldn't want to load different sizes of the same texture into memory as that would be a waste.缩放图像通常不会产生那么多开销,另外,您不希望将相同纹理的不同大小加载到 memory 中,因为那会浪费。

Of course, you could be writing a Zune game, which means I'm probably off about the scaling there, but, anyway.当然,您可能正在编写 Zune 游戏,这意味着我可能对那里的缩放不感兴趣,但是,无论如何。

I don't think you can save the scaled image directly from XNA, but, you could use WinForms Image class to load the image and then scale it to the dimensions in your level editor and save it back out to disk.我认为您不能直接从 XNA 保存缩放后的图像,但是,您可以使用 WinForms Image class 加载图像,然后将其缩放到关卡编辑器中的尺寸并将其保存回磁盘。

If your texture isn't a BMP or PNG though, you may have to use the Texture2D.Save() command to convert it into a format you can use in the Image class.如果您的纹理不是 BMP 或 PNG,您可能必须使用 Texture2D.Save() 命令将其转换为可在图像 class 中使用的格式。

This thread might help you with using the Image class.该线程可能会帮助您使用图像 class。 http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/6fe3d353-0b09-440b-95c9-701efdc9e20a/ http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/6fe3d353-0b09-440b-95c9-701efdc9e20a/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM