I am trying to make a Texture2D from a Bitmap. I have
Texture2D BitmapToTexture(Bitmap img)
{
var ret = new Texture2D(Game.GraphicsDevice, img.Width, img.Height);
var bd = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
int[] bytes = new int[img.Width * img.Height];
Marshal.Copy(bd.Scan0, bytes, 0, bytes.Length);
ret.SetData(bytes);
img.UnlockBits(bd);
return ret;
}
The issue is that SetData is for some reason expec开发者_如何学编程ting ABGR. Is there a way to get SetData to take ARGB data?
Unfortunately not. You have to swap the bytes yourself.
The byte ordering was changed in XNA 4.0 (see last paragraph).
See also this answer.
精彩评论