Skip to main content
GameDev.net gamedev.net
🔒 Locked

C#, DirectX, 2D, and PNG images... Wha?!

Started by NVergunst Jan 26, 2008 at 5:05 PM 4 replies 7.8k views
Original Post
NVergunst
NVergunst
Ok, I have been reading up on lots and lots of pages on the web about using DirectX. I have created a device using both DirectDraw and Direct3D. All I want to do is put a static PNG image with transparency (not just on/off transparency, but every pixel has an alpha channel) on the screen. That is it. It seems like everything is geared towards the 2 extremes. Either fullblown 3D (which I dont want or need), or 2D things like pong, which use regular bitmaps with no transparency, or colour code transparency where a certain colour is rendered transparent. Neither of that is what I want. I just want a png, on the surface, that is it, and I am coming up blank. I saw a few posts regarding DX8 such as this, "Direct3dx can load in png files that have alpha channels int an id3dxtexture9 interface. look up d3dxLoadTextureFromFileEx". I am using C#, and I do not see any of those classes/structs/API calls or whatever they are. It seems like a very basic question, with a more than likely basic answer, but I cannot for the life of me find the answer. A bit of background on my application: Originally developed in C# using the default GDI+ graphics, but was just too darned slow. So I moved to XNA because it seemed the perfect solution. Problem is it is not compatible with any older hardware, or even some new hardware, and requires about 5 different redistributables to be of any use (.NET 2.0, VC++ 2005, XNA 2.0, DX + Optional DX files through the web installer which cannot be included as a redistributable). So I am trying to move to OpenGL or DirectX. Which I do not know. I donwloaded the Tao Framework for OpenGL, but in Vista with Aero, it renders all wrong. If Aero is off, it renders fine, but I want an all inclusive solution which doesnt force people to take Aero off. And it is a windowed application, so that is why it wont work. I know in fullscreen mode, Aero releases its hooks in the video card allowing OpenGL to work, but while Aero is running it just doesnt work. So now I am exploring DirectX. I have my device setup, and I can clear and flash random colours on the screen. All I need now is some way to put "image.png" at location 0,0 with a size of 100,100 for example. Any help? Thankyou in advance.
ViLiO
ViLiO
Well you'd need to load your .png as a Texture, and then you could just use the Sprite Class to draw it on screen.

A quick google should give you a tonne of source code examples on how to use it.

Regards,
ViLiO
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
NVergunst
NVergunst
Ok, thankyou. Texture was the keyword I needed. I found lots on surfaces, sprites, quads and such, but nobody mentioned textures as something like that. I will do some googling and see what I come up with. Just looking at the MSDN, it looks like it takes a Bitmap or a stream correct? The Bitmap is the bitmap as seen by C# correct like the Image. So I could do "= new Texture(mydevice, new Image.FromFile("imagepath.png"), Usage.SomeValue, Pool.SomeValue)" correct?

Is there anything I should know regarding the Usage and Pool for 2D images before I start fiddling with stuff? :)
ViLiO
ViLiO
For loading the Texture you can just use TextureLoader.FromFile (which 404s for me ...the docs on msdn for MDX 1.1 must be starting to die already [sad])

If your image doesn't have power-of-two dimensions, you should also use TextureLoader.ImageInformationFromFile and then use the data it returns as the TextureLoader.FromFile parameters. If you don't do this there is a strong chance it will scale your texture to the nearest power-of-two.

With regards to Usage and Pool flags, I think you should just use Usage.WriteOnly and Pool.Managed.

Something that you might consider for the future (and definitely to aid with distributing your app) would be to move to SlimDX instead of MDX 1.1

The reason for this is that MDX 1.1 is dead as Microsoft have moved focus to the XNA Framework (not really a bad thing as XNA does rock quite a lot [grin]). On the other hand, SlimDX is under active development and quite frankly a lot nicer to work with than MDX 1.1 ever was [smile]

All the best,
ViLiO
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
NVergunst
NVergunst
Oh my god, thankyou. I give you a virtual cookie. :)

It works. Now to experiment with overlapping pngs, but the basics are there now, and I cannot be happier on how easy it really was. I feel as if the simplest things like these are overlooked in tutorials that are easy accessible on the web unfortuneatly. There is plenty of good programming theory, basic basics about initializing the device, performance differences with flipping, and windowed vs. fullscreen mode, but then simple things that I am sure many people are looking for are just glossed over and expected to be known. Then the 3D world begins with a real lack of info for the middle jump. This is just what I need, and thanks again.

I hope someone else in their googling will find this when they are starting to learn DirectX.

This is what I have used for testing by the way:

mySprite = new Sprite(myDevice);
myNTexture = new Texture(myDevice, new Bitmap(Image.FromFile("N.png")), Usage.Dynamic, Pool.Default);

and then in the Render loop, I have:

myDevice.Clear(ClearFlags.Target, Color.FromArgb(argb32), 1.0f, 0);
myDevice.BeginScene();
mySprite.Begin(SpriteFlags.AlphaBlend);
mySprite.Draw2D(myNTexture, new Point(0, 0), 0.0f, new Point(10, 10), Color.White);
mySprite.End();
myDevice.EndScene();
myDevice.Present();
NVergunst
NVergunst
Ok, I see I do need to use the texture loader because it is scaling it to the nearest power of 2. But at least I know I am not crazy. Thanks again!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.