Before you delve into the details of WIC, you may consider to encapsulate byte swapping in a simple custom BitmapSource like shown below. It takes an RGBA byte array and swaps the pixel bytes in the overridden CopyPixels
method to produce a PBGRA buffer.
You can simply create an instance of this custom BitmapSource by providing the RGBA buffer and the bitmap width like
var buffer = new byte[] { 25, 166, 0, 255, 90, 0, 120, 255 };
var bitmapSource = new RgbaBitmapSource(buffer, 2);
Here is the implementation:
public class RgbaBitmapSource : BitmapSource
{
private byte[] rgbaBuffer;
private int pixelWidth;
private int pixelHeight;
public RgbaBitmapSource(byte[] rgbaBuffer, int pixelWidth)
{
this.rgbaBuffer = rgbaBuffer;
this.pixelWidth = pixelWidth;
this.pixelHeight = rgbaBuffer.Length / (4 * pixelWidth);
}
public override void CopyPixels(
Int32Rect sourceRect, Array pixels, int stride, int offset)
{
for (int y = sourceRect.Y; y < sourceRect.Y + sourceRect.Height; y++)
{
for (int x = sourceRect.X; x < sourceRect.X + sourceRect.Width; x++)
{
int i = stride * y + 4 * x;
byte a = rgbaBuffer[i + 3];
byte r = (byte)(rgbaBuffer[i] * a / 256); // pre-multiplied R
byte g = (byte)(rgbaBuffer[i + 1] * a / 256); // pre-multiplied G
byte b = (byte)(rgbaBuffer[i + 2] * a / 256); // pre-multiplied B
pixels.SetValue(b, i + offset);
pixels.SetValue(g, i + offset + 1);
pixels.SetValue(r, i + offset + 2);
pixels.SetValue(a, i + offset + 3);
}
}
}
protected override Freezable CreateInstanceCore()
{
return new RgbaBitmapSource(rgbaBuffer, pixelWidth);
}
public override event EventHandler<DownloadProgressEventArgs> DownloadProgress;
public override event EventHandler DownloadCompleted;
public override event EventHandler<ExceptionEventArgs> DownloadFailed;
public override event EventHandler<ExceptionEventArgs> DecodeFailed;
public override double DpiX
{
get { return 96; }
}
public override double DpiY
{
get { return 96; }
}
public override PixelFormat Format
{
get { return PixelFormats.Pbgra32; }
}
public override int PixelWidth
{
get { return pixelWidth; }
}
public override int PixelHeight
{
get { return pixelHeight; }
}
public override double Width
{
get { return pixelWidth; }
}
public override double Height
{
get { return pixelHeight; }
}
}
As pointed out in a comment, you might improve performance by implementing an unsafe copy operation, which could look like this:
unsafe public override void CopyPixels(
Int32Rect sourceRect, Array pixels, int stride, int offset)
{
fixed (byte* source = rgbaBuffer, destination = (byte[])pixels)
{
byte* dstPtr = destination + offset;
for (int y = sourceRect.Y; y < sourceRect.Y + sourceRect.Height; y++)
{
for (int x = sourceRect.X; x < sourceRect.X + sourceRect.Width; x++)
{
byte* srcPtr = source + stride * y + 4 * x;
byte a = *(srcPtr + 3);
*(dstPtr++) = (byte)(*(srcPtr + 2) * a / 256); // pre-multiplied B
*(dstPtr++) = (byte)(*(srcPtr + 1) * a / 256); // pre-multiplied G
*(dstPtr++) = (byte)(*srcPtr * a / 256); // pre-multiplied R
*(dstPtr++) = a;
}
}
}
}