Convert BGR to RGB in Texture2D from unmanaged memory

You could mod the array before setting it to your uiTexture to rearrange the R/B values.

Though the method I would recommend would be a simple shader. Here’s one that would swap the R/B values (happens at draw time).

(untested, but it’s quite simple so should be fine honestly)

#if OPENGL
	#define SV_POSITION POSITION
	#define VS_SHADERMODEL vs_3_0
	#define PS_SHADERMODEL ps_3_0
#else
	#define VS_SHADERMODEL vs_4_0 //_level_9_1
	#define PS_SHADERMODEL ps_4_0 //_level_9_1
#endif


Texture2D SpriteTexture;
sampler2D SpriteTextureSampler = sampler_state { Texture = <SpriteTexture>; };


struct VertexShaderOutput
{
	float4 Position : SV_POSITION;
	float4 Color : COLOR0;
	float2 TextureCoordinates : TEXCOORD0;
};


float4 MainPS(VertexShaderOutput input) : COLOR
{
	float4 color = tex2D(SpriteTextureSampler, input.TextureCoordinates);

	float rBuffer = color.r;
	color.r = color.b;
	color.b = rBuffer;

	return color;
}


technique SpriteDrawing
{
	pass P0
	{
		PixelShader = compile PS_SHADERMODEL MainPS();
	}
}

Put that in a “rbSwapShader.fx” file and add to your content builder, It can then be used like so:

shader = Content.Load<Effect>("rbSwapShader");

spriteBatch.Begin(
	sortMode: spriteSortMode,
	blendState: blendState ?? BlendState.AlphaBlend,
	effect: shader
);
1 Like