Converting System.Drawing.Bitmap to IDirect2D1Bitmap

Started by
1 comment, last by ta0soft 10 years, 1 month ago

Like the title says I'm trying to convert my System.Drawing.Bitmaps (with alpha) to IDirect2D1Bitmaps in C#, specifically a SharpDX.Direct2D1.Bitmap but I don't think that matters.

To the best of my knowledge GDI+ bitmaps are stored in pre-multiplied BGRA format and I need to keep this same format when converting them to D2D bitmaps, that being DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED. All of the samples included with SharpDX convert the BGRA values to ARGB first before creating the bitmaps so I need a different solution.

The reason is because I'm trying to render them inside an aero glass window using Direct2D. The only way to accomplish this is with pre-multiplied BGRA pixel format, and according to Microsoft pre-multiplied BGRA is the most efficient way to draw in Direct2D. I normally just use ARGB format but alpha blending doesn't work correctly on glass windows.

I came up with a few different conversion functions but none of them seem to work properly. The alpha values are getting lost somewhere during conversion. The color channels seem to convert fine but the resulting bitmap always has 100% alpha even when the original image has transparency.

I don't have this problem using RGBA the alpha channels are converted perfectly, but when I use BGRA it doesn't work. My best guess is because the values are already pre-multiplied but I'm not sure how to convert them correctly.

The first function is the most efficient because it doesn't require locking the System.Drawing.Bitmap, but they all do the same thing. The last function confuses me the most because it copies the bits directly and it still doesn't work.

[source]
BitmapProperties bp = new BitmapProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied));

int stride = bmp.Width * sizeof(int);

using (DataStream tempStream = new DataStream(bmp.Height * stride, false, true))

{

for (int y = 0; y < bmp.Height; y++)

{

for (int x = 0; x < bmp.Width; x++)

{

Color c = bmp.GetPixel(x, y);

int bgra = c.B | (c.G << 8) | (c.R << 16) | (c.A << 24);

tempStream.Write(bgra);

}

}

return new SharpDX.Direct2D1.Bitmap(renderTarget, new DrawingSize(bmp.Width, bmp.Height), tempStream, stride, bp);

}
[/source]

[source]

BitmapProperties bp = new BitmapProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied));

int stride = bmp.Width * sizeof(int);

using (DataStream tempStream = new DataStream(bmp.Height * stride, false, true))

{

BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);

for (int y = 0; y < bmp.Height; y++)

{

int offset = bmpData.Stride * y;

for (int x = 0; x < bmp.Width; x++)

{

byte b = Marshal.ReadByte(bmpData.Scan0, offset++);

byte g = Marshal.ReadByte(bmpData.Scan0, offset++);

byte r = Marshal.ReadByte(bmpData.Scan0, offset++);

byte a = Marshal.ReadByte(bmpData.Scan0, offset++);

int bgra = b | (g << 8) | (r << 16) | (a << 24);

tempStream.Write(bgra);

}

}

bmp.UnlockBits(bmpData);

return new SharpDX.Direct2D1.Bitmap(renderTarget, new DrawingSize(bmp.Width, bmp.Height), tempStream, stride, bp);

}
[/source]

[source]

BitmapProperties bp = new BitmapProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied));

int stride = bmp.Width * sizeof(int);

using (DataStream tempStream = new DataStream(bmp.Height * stride, false, true))

{

BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);

int numBytes = bmpData.Stride * bmp.Height;

byte[] byteData = new byte[numBytes];

IntPtr ptr = bmpData.Scan0;

Marshal.Copy(ptr, byteData, 0, numBytes);

bmp.UnlockBits(bmpData);

tempStream.Write(byteData, 0, numBytes);

return new SharpDX.Direct2D1.Bitmap(renderTarget, new DrawingSize(bmp.Width, bmp.Height), tempStream, stride, bp);

}
[/source]

Advertisement

Any advice? I tried everything I still can't figure it out. Looks like I'm stuck using RGBA for the time being :(

I finally figured it out thanks to good-old MSDN, I guess I should do more homework before asking these types of questions blink.png

"To make a composite image in the standard RGBA format, the alpha value of the foreground image must be multiplied by each of the red, green, and blue channels before adding it to the color of the background image. In a pre-multiplied alpha RGB pixel format, each color channel has already been multiplied by the alpha value. This provides a more efficient method of image composition with alpha-channel data. To retrieve the true color values of each channel in a PRGBA/PBGRA pixel format, the alpha-channel multiplication must be reversed by dividing color values by the alpha value."

Here's the working code for anyone who might be having the same issue:

[source]

BitmapProperties bp = new BitmapProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied));

int stride = bmp.Width * sizeof(int);

using (SharpDX.DataStream tempStream = new SharpDX.DataStream(bmp.Height * stride, false, true))

{

for (int y = 0; y < bmp.Height; y++)

{

for (int x = 0; x < bmp.Width; x++)

{

Color c = bmp.GetPixel(x, y);

int a = c.A;

int r = (c.R * a) / 255;

int g = (c.G * a) / 255;

int b = (c.B * a) / 255;

int bgra = b | (g << 8) | (r << 16) | (a << 24);

tempStream.Write(bgra);

}

}

return new SharpDX.Direct2D1.Bitmap(renderTarget, new DrawingSize(bmp.Width, bmp.Height), tempStream, stride, bp);

}

[/source]

This topic is closed to new replies.

Advertisement