From a2720eb2c5e6fd35916e6d6983d79b3d1a3167c7 Mon Sep 17 00:00:00 2001 From: Alexander Eckart Date: Wed, 17 Apr 2013 18:09:11 +0200 Subject: [PATCH] fixed texture scaling --- Vocaluxe/Lib/Draw/CDirect3D.cs | 40 +++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/Vocaluxe/Lib/Draw/CDirect3D.cs b/Vocaluxe/Lib/Draw/CDirect3D.cs index 63672c7f..057f1ff7 100644 --- a/Vocaluxe/Lib/Draw/CDirect3D.cs +++ b/Vocaluxe/Lib/Draw/CDirect3D.cs @@ -932,11 +932,41 @@ namespace Vocaluxe.Lib.Draw h = maxSize; } - //Fill the new Bitmap with the texture data - BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); - byte[] data = new byte[4 * w * h]; - Marshal.Copy(bmpData.Scan0, data, 0, data.Length); - bmp.UnlockBits(bmpData); + //Older graphics card can only handle textures with sizes being powers of two + w = (int)_CheckForNextPowerOf2(w); + h = (int)_CheckForNextPowerOf2(h); + + Bitmap bmp2 = null; + byte[] data; + try + { + if (w != bmp.Width || h != bmp.Height) + { + //Create a new Bitmap with the new sizes + bmp2 = new Bitmap(w, h); + //Scale the texture + Graphics g = Graphics.FromImage(bmp2); + g.InterpolationMode = InterpolationMode.HighQualityBicubic; + g.SmoothingMode = SmoothingMode.HighQuality; + g.DrawImage(bmp, new Rectangle(0, 0, bmp2.Width, bmp2.Height)); + g.Dispose(); + bmp = bmp2; + } + + //Fill the new Bitmap with the texture data + BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); + data = new byte[4 * w * h]; + Marshal.Copy(bmpData.Scan0, data, 0, data.Length); + bmp.UnlockBits(bmpData); + } + finally + { + if (bmp2 != null) + bmp2.Dispose(); + } + + if (data == null) + return new STexture(-1); return AddTexture(w, h, ref data); }