2012-04-08 88 views
1

我找到了直接从文件加载图像的方式,但我加载的图像是蓝色的(原始为绿色)。我想它保存的方式很糟糕,所以我用photoshop保存了它,但没有任何改变。我想我的程序工作不好。我怎样才能改变它,是从文件中加载图像的好方法? 位图的Texture2D方法:XNA和从文件加载图像

public static Texture2D GetTexture2DFromBitmap(GraphicsDevice device, Bitmap bitmap) 
    { 
     Texture2D tex = new Texture2D(device, bitmap.Width, bitmap.Height); 
     System.Drawing.Imaging.BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat); 
     int bufferSize = data.Height * data.Stride; 
     byte[] bytes = new byte[bufferSize]; 
     System.Runtime.InteropServices.Marshal.Copy(data.Scan0, bytes, 0, bytes.Length); 
     tex.SetData(bytes); 
     bitmap.UnlockBits(data); 
     return tex; 
    } 

导入图像行:

 backgroundTexture = Tools.GetTexture2DFromBitmap(device, (System.Drawing.Bitmap)System.Drawing.Image.FromFile(@"1.bmp", false)); 

溺水纹理的方法:

 spriteBatch.Begin(); 
     Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight); 
     spriteBatch.Draw(backgroundTexture, screenRectangle, Color.White); 
     spriteBatch.End(); 

回答

2

嗯....我认为是比较容易通过加载图像文件Texture2D.FromStream方法...

texture = Texture2D.FromStream(Device, File.OpenRead(路径));

是的,它只加载jpg,png和gif图像,但最新的情况如何? ... 转换bmp很简单..