System.Drawing.Graphics and Texture2D?

Started by
2 comments, last by DvDmanDT 11 years, 11 months ago
Hi, I'm currently writing some tools for my XNA games and started to miss System.Drawing.Graphics and started to wonder if I can use it in XNA somehow? Either to draw to the backbuffer or to a texture/rendertarget..? (I'm guessing no, but I figured I might as well ask)
Advertisement
I haven't tried this, but you may be able to:

  • Make a Bitmap using one of the size and pixelformat constructors.
  • Use Graphics.FromBitmap.
  • Draw using the Graphics object.
  • Dispose the Graphics object (I would wrap the whole 'Graphics' operation in a using block).
  • Use Bitmap.LockBits and grab the pixel data out, and transfer them over to a Texture2D.

I'm also guessing that System.Drawing.Graphics is only available on Windows, and not 360 or WP7. You'll have to double check.

Depending on what you're using it for, performance might not be adaquate. Though, give it a try to find out.
You could import it, and maybe work out how to use it, but I wouldn't recommend it. Between a lack of double buffering and that it's not optimized for game code could leave you with flickering and laggy graphics. I am also unsure how well that would port to Xbox if you were to do so.
It wouldn't port to other platforms, but it's for tools so I don't really care. Also, I'm not talking about drawing the actual game this way, I just want to place markers and such in my editor.

Speed is also not really much of a concern, and it's also not as slow as one might think. I once replaced an applications drawing code with D3D. There were thousands (more like 50k) of axis aligned rectangles and lines drawn, each with their own draw call, so I figured I could probably get a significant boost by drawing colored quads and lines in D3D. I performed batching, cached vertex/index buffers, tried with and without indices, I tried userprimitives and so on, and my best attempt got me something like 5-10% performance gain. At that point I figured GDI+ is pretty well optimized for this kind of drawing.

Just out of curiousity, if I used Direct3D instead of XNA, could I get a device context from a texture (as in HDC from WinApi)?

This topic is closed to new replies.

Advertisement