0

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.

zemaster
  • 1
  • 3
  • Having thought about it further, simply swapping the two Matrices around (multiply the Scaling by the Translation) *should* make it work. This is effectively perform the translation second, so it won't be affected by the scale, which is what I think is happening now. – VisualMelon Jan 07 '13 at 17:49
  • Yeah, I'm not sure what is going on, so I'm just going to resort to my workaround because I'm not resizing every frame so it's not a big deal. – zemaster Jan 08 '13 at 00:46

1 Answers1

0

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();
        }
    }
}
zemaster
  • 1
  • 3