# C#, managed Directx, and colorkeys [solved]

This topic is 3665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, I'm making the switch from C++ to C# and I'm having a little trouble with texturing. I'm just doing simple 2D with the D3D Sprite interface. I've been using this in C++ for a while, but I can't seem to get the colorkey transparency working in managed DX. Here's the loading code:
d3dTex[2] = TextureLoader.FromFile(d3dDevice,
Application.StartupPath + @"\..\..\images\buttons.png", 0, 0, 1, 0,
Format.A8B8G8R8, Pool.Default, Filter.None, Filter.None, 0xff00ff);


The color key parameter is the last one, 0xff00ff. If I use 0xffff00ff like I did in C++ I get an invalid parameter error. It compiles and runs fine with 0xff00ff, but the purple color becomes black, not transparent. I tried setting the ALPHABLENDENABLE and ALPHATESTENABLE states but that didn't fix it. Any ideas why it's turning that color black instead of transparent? [Edited by - Spencer Bowers on June 11, 2008 8:54:39 AM]

##### Share on other sites

Try using unchecked((int)0xffff00ff) for that last parameter. Because of overflow checking at compile time, C# will balk at this parameter not being a proper int value. Using this unchecked cast you can make it an int parameter, while keeping the proper layout DX needs.

At least, I hope this helps [smile]

##### Share on other sites
If that doesn't help the FAQ has a big section on alpha blending which you might find useful.

##### Share on other sites
Quote:
 Original post by remigiusTry using unchecked((int)0xffff00ff) for that last parameter. Because of overflow checking at compile time, C# will balk at this parameter not being a proper int value. Using this unchecked cast you can make it an int parameter, while keeping the proper layout DX needs.At least, I hope this helps [smile]

Well that satisfies the compiler so I don't get the error, but the results are the same as just using 0xff00ff. It seems like this is the right way to do it since DX is actually changing the color from purple to black, but it should be transparent black rather than opaque.

Also, thanks for pointing me to the FAQ but the alpha blending section only mentions color keys. It doesn't explain how to use them at all. Any other ideas?

##### Share on other sites
I don't remember having to do this in C++, but it fixed the problem, so whatever. I set the blending render states and it comes out transparent as it should:

d3dDevice.RenderState.AlphaBlendEnable = true;d3dDevice.RenderState.SourceBlend = Blend.SourceAlpha;d3dDevice.RenderState.DestinationBlend = Blend.InvSourceAlpha;

I wasn't setting SourceBlend and DestinationBlend blend before.

##### Share on other sites
Maybe in C++ it was being set elsewhere and you forgot about it? You can't have transparency/colorkeying without either alpha test or alpha blend enabled, after all.

1. 1
2. 2
3. 3
4. 4
Rutin
18
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631423
• Total Posts
3000003
×