Sign in to follow this  
zangetsu

[.net] XNA Prevent Texture Stretch When Loading From File

Recommended Posts

I've got some irregularrly sized images that I am trying to load as a texture in XNA and I've noticed that even if I supply the hight and width parameters to the FromFile method, the resulting texture always reports the resulting texture as having a size that is in a multiple of 2. For example a source bitmap that is 128x192, becomes 128x256 when loaded as an XNA texture. Whats annoying here is that the image file is already in the resolution I want to draw at, so if I correct using the source and destination rectangles that compensate for the stretch, distortion artifacts are introduced. When the I was using MDX 1.1, I could create irregular size textures no problem, and no distortion artifacts were introduced. So Aside from resizing the images, how can I draw and avoid the distortion?

Share this post


Link to post
Share on other sites
It maybe that your graphics card only supports power of 2 texture sizes (but you'd prob get an exception if you tried to load a non power of 2)

I'm pretty sure MDX had this problem, the way to solve it in MDX was to use 'new Texture( Device, System.Drawing.Bitmap, ... )' function. Loading the texture from a bitmap seemed to allow any width and height. However i think XNA has gotten rid of any System.Drawing references :(

Share this post


Link to post
Share on other sites
I'm pretty sure that the Video card can do unusall texture sizes. As far as for creating those sizes in MDX, I was using new Texture( Device, System.Drawing.Bitmap, ... ) as you pointed out.

I have a feeling that the reason for taking out all the System.Drawing references is that they don't want to minimal dependancies for other platforms (XBox). Its rather annoying for those of us that are OK with developing for the PC exclusively. Its nuts that I have to have a helper function that converts System.Drawing.Color and Microsoft.Xna.Framework.Graphics.Color and vice versa, when they both have exactly the same data, (RGBA Values). The Rectangle Structure is in the same boat.

Share this post


Link to post
Share on other sites
Of course just after I finish bitching about things. I did realize that the TextureCreationParameters allows me to set a filter. Setting that filter to point causes the texture to come out looking like the origional despite being stretched to a power of 2, then being stretched back to its native size.

Point - Each destination pixel is computed by sampling the nearest pixel from the source image.


CreateParams = TextureCreationParameters.Default
CreateParams.ColorKey = New Color(255, 0, 0)
CreateParams.Filter = FilterOptions.Point
MyTexture = Texture2D.FromFile(MyDevice, "terra.bmp", CreateParams)


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this