2012-04-08 117 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很簡單..