• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ta0soft

Converting System.Drawing.Bitmap to IDirect2D1Bitmap

2 posts in this topic

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]

Edited by ta0soft
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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]

Edited by ta0soft
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0