我找到了直接从文件加载图像的方法,但是我加载的图像是蓝色的(原始图像是绿色的)。我坚持认为它的保存方式很糟糕,所以我用 Photoshop 保存了它,但没有任何改变。我猜我的程序运行不好。我该如何更改它,这是从文件加载图像的好方法吗?位图到 texture2d 方法:
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();