Will
Will

Reputation: 10401

C# Texture2D SetData ARGB?

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 expecting ABGR. Is there a way to get SetData to take ARGB data?

Upvotes: 1

Views: 2449

Answers (1)

Andrew Russell
Andrew Russell

Reputation: 27195

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.

Upvotes: 2

Related Questions