Resizing and position a SharpDX sprite

两盒软妹~` 提交于 2019-12-13 05:19:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!