问题
I'm trying to resize a DirectX Texture and place it in the top right corner of the window. I am drawing the texture using a sprite, here's is my code (I am using SharpDX):
albumArtSprite.Begin();
NativeMethods.RECT rect;
NativeMethods.GetClientRect(device.CreationParameters.HFocusWindow, out rect);
float targetDimensions = 150f;
var matrix = Matrix.Scaling(targetDimensions / albumArtInformation.Width, targetDimensions / albumArtInformation.Height, 0f) *
Matrix.Translation(rect.Width - albumArtInformation.Width - 10f, 10f, 0f);
albumArtSprite.Transform = matrix;
albumArtSprite.Draw(albumArtTexture, new ColorBGRA(0xFFFFFFFF));
albumArtSprite.End();
For some reason, I'm getting really strange results like the image not being where I want it to be which is in the top-right hand corner of the window with an offset of 10 on the X and Y axis. It's my first time working with DirectX so I'm not 100% sure about this matrix stuff.
回答1:
In the end, I resorted to using the Bitmap and Graphics class to resize the image before drawing it.
private static byte[] ResizeAlbumArt(byte[] originalAlbumArtData)
{
using (var originalStream = new MemoryStream(originalAlbumArtData))
{
var original = new Bitmap(originalStream);
var target = new Bitmap(AlbumArtDimensions, AlbumArtDimensions);
using (var g = Graphics.FromImage(target))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(original, 0, 0, target.Width, target.Height);
}
using (var outputStream = new MemoryStream())
{
target.Save(outputStream, ImageFormat.Bmp);
return outputStream.ToArray();
}
}
}
来源:https://stackoverflow.com/questions/14183861/resizing-and-position-a-sharpdx-sprite