Bạn có thể chia tỷ lệ kết cấu của mình bằng cách hiển thị cho mục tiêu hiển thị ở kích thước bạn muốn và sau đó lưu kết cấu mục tiêu hiển thị.
Ví dụ đơn giản này cho thấy cách bạn có thể thực hiện điều đó. Bỏ qua các thiết lập của GraphicsDevice, đó là chỉ để làm một ví dụ nhỏ khép kín. Bit thú vị là tạo ra các mục tiêu render và vẽ kết cấu thu nhỏ. Bạn nên sử dụng lại các mục tiêu hiển thị nơi bạn có thể (tất cả các hình ảnh có cùng kích thước có thể sử dụng lại mục tiêu hiển thị).
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);
}
}
Nguồn
2009-04-01 13:24:32
Đây là điều gần nhất với những gì tôi đã kết thúc khi triển khai. Mặc dù tôi không sử dụng nhập kernel32 hoặc GetConsoloWindow, tôi chỉ đơn giản sử dụng GraphicsDevice từ đối tượng Game của mình. –