• Advertisement
Sign in to follow this  

Texture.FromFile() alternative: sweet snippet!

This topic is 3491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

following my exhausting attempts to find a memory leak, which was caused by TextureLoader.FromFile() I had to find an alternative by using the Texture.FromFile() which rendered the debug process to almost impossible and the runtime hundreds percents slower, I decided to write my own Texture.FromBitmap() method. Bugless this time. It took me about 2-3 days, so I decided to post it in favor of other annoyed programmers. It's just as fast as TextureLoader.FromFile(), it can't load TGA's but it can handle BMP, PNG, PCX and JPG. enjoy.

// Load Bitmap
Bitmap bitfile = new Bitmap(Image.FromFile(sTextureName));
Rectangle sourceRect = new Rectangle(0, 0, bitfile.Width, bitfile.Height);
                       
// Calculate the texture size in power of 2 for a wider gfx card support
int l_Width, l_Height;
for (l_Width = 2; l_Width < bitfile.Width; l_Width *= 2 ) ;
for (l_Height = 2; l_Height < bitfile.Height; l_Height *= 2) ;
                        
// lock bitmap data and texture data
BitmapData l_BitmapData =
           bitfile.LockBits(new Rectangle(0, 0, bitfile.Width, bitfile.Height),
                                             ImageLockMode.ReadWrite,
                                             PixelFormat.Format32bppArgb);

// Create texture
Format l_TextureFormat = Format.A8R8G8B8;
                        
tex = new Texture(Graphics.D3DDevice,
                  l_Width,
                  l_Height,
                  1,
                  Usage.None,
                  l_TextureFormat,
                  Pool.Managed);

                        
uint[,] l_TextureData = (uint[,])tex.LockRectangle(typeof(uint), 0,
           LockFlags.None,
           new int[] { tex.GetSurfaceLevel(0).Description.Height, 
                                                                                       tex.GetSurfaceLevel(0).Description.Width });
                        
// Copy pixels from bitmap to texture
unsafe
{
      int l_pixel = 4;
      for (int l_h = 0; l_h < sourceRect.Height; l_h++)
            {
            for (int l_w = 0; l_w < sourceRect.Width; l_w++)
                  {
                  byte* l_pointer = (byte*) (void*) l_BitmapData.Scan0 +
                                                      (l_BitmapData.Stride*l_h) + (l_w*l_pixel);
                                    l_TextureData[l_h, l_w] = (uint) Color.FromArgb(
                                                                        l_pointer[3],
                                                                        l_pointer[2],
                                                                        l_pointer[1],
                                                                        l_pointer[0]
                                                                        ).ToArgb();
                                    
        }
    }
}
                        
// unlock the bitmap and the texture
bitfile.UnlockBits(l_BitmapData);
tex.UnlockRectangle(0);
                        
bitfile.Dispose();


Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Mike.Popoloski
Or you could stop using an outdated and deprecated API and switch to the XNA Framework. [smile]


Fixed that for you. [grin]

Share this post


Link to post
Share on other sites
Quote:
Original post by Machaira
Quote:
Original post by Mike.Popoloski
Or you could stop using an outdated and deprecated API and switch to the XNA Framework. [smile]


Fixed that for you. [grin]


the project works on windows XP embedded, which AFAIK, has no XNA support, and even if it had, the gfx card doesn't support DX10

Share this post


Link to post
Share on other sites
Quote:
Original post by Mike.Popoloski
Or you could stop using an outdated and deprecated API and switch to SlimDX. [smile]


API burn!


Quote:
Original post by Machaira
Quote:
Original post by Mike.Popoloski
Or you could stop using an outdated and deprecated API and switch to the XNA Framework. [smile]


Fixed that for you. [grin]


Double API burn!!

Share this post


Link to post
Share on other sites
Quote:
Original post by Gilzu
Quote:
Original post by Machaira
Quote:
Original post by Mike.Popoloski
Or you could stop using an outdated and deprecated API and switch to the XNA Framework. [smile]


Fixed that for you. [grin]


the project works on windows XP embedded, which AFAIK, has no XNA support, and even if it had, the gfx card doesn't support DX10


For what it's worth, XNA doesn't require (and indeed, doesn't even support) D3D10.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement