È possibile ridimensionare le trame eseguendo il rendering su un oggetto di rendering con le dimensioni desiderate e salvando la trama di destinazione del rendering.
Questo semplice esempio mostra come è possibile farlo. Ignora la configurazione di GraphicsDevice, che è solo per fare un piccolo esempio autonomo. Il bit interessante è la creazione del target di rendering e il disegno della texture in scala. Dovresti riutilizzare i target di rendering dove puoi (tutte le immagini della stessa dimensione possono riutilizzare un target di rendering).
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);
}
}
fonte
2009-04-01 13:24:32
Questo è il più vicino a quello che ho finito per implementare. Sebbene non abbia usato l'importazione kernel32 o GetConsoloWindow, ho semplicemente usato il mio GraphicsDevice dal mio oggetto Game. –