Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Before I get started I would just like to say that I have been using the Microsoft Winform + Xna examples found here:

WinForms Series 1: Graphics Device

I think I somewhat understand how its working however I'm running into one major problem. I'm trying to draw a Texture2D image to the spriteFontControl's spriteBatch parameter, which is an object of the XNA SpriteBatch class, and when I do I get this red "X" on a white box that fills the entire spriteFontControl area. This is a screen shot of what it looks like:


As for the coding that I added to do this is pretty simple. I used the content parameter from the spriteFontControl to load the box.jpg image into the program. Then just called the following code inside the Draw() method for the spriteFontControl:

spriteBatch.Draw(box, new Vector2(0,0),Color.Black);

If anyone could please offer some help as to how to draw Texture2D images in the Winform + XNA example then that would be most helpful. I heard that it was made so that you could draw it just like you would in XNA but I think I'm still missing something.

Thanks in advance.

P.S. This is the code from the Draw() method of the spriteFontControl:

    const string message = "Hello, World!\n" +
                               "\n" +
                               "I'm an XNA Framework GraphicsDevice,\n" +
                               "running inside a WinForms application.\n" +
                               "\n" +
                               "This text is drawn using SpriteBatch,\n" +
                               "with a SpriteFont that was loaded\n" +
                               "through the ContentManager.\n" +
                               "\n" +
                               "The pane to my right contains a\n" +
                               "spinning 3D triangle.";


    spriteBatch.Draw(box, new Vector2(15, 15), Color.Black);
    spriteBatch.DrawString(font, message, new Vector2(23, 23), Color.White);
This is a simple debug your code issue. Place a breakpoint in your Paint function and step through. –  Luis Estrada May 4 '13 at 16:48

2 Answers 2

up vote 0 down vote accepted

you are throwing an exception...

you should ensure that "spritebatch" and "box" variables are not null.

Wait so its drawing the "X" on screen because either spritebatch or box is null? I don't think spriteBatch is null because the program calls spriteBatch.DrawString earlier in the function which worked fine. From what I've seen box shouldn't be null either. I was using: box = content.Load<Texture2d>("box"); –  Daniel Miles May 4 '13 at 16:45
you have an unhandle exception for sure, without seeing more code is dificult to tell you where is the bug, but it should be easy to debug in your draw method. –  Blau May 4 '13 at 17:18
put breakpoint and debug it, if you have an update method it can contain the bug too.. –  Blau May 4 '13 at 19:06
Wow. I just can't say how much I'm starting to learn now. I went poking around in the coding for the Winform/Xna example by Microsoft one more time and I was finally able to fix the problem. I found out that I wasn't adding the "box" image to the Content portion of the program. I apparently hit "Add Existing Item" in the wrong part of the Solution Viewer and therefore the box file could not be found. Tried it again and my box popped up :). Thank you again for all of your help. –  Daniel Miles May 6 '13 at 4:59

Instead of creating a game in a form, create a form in a game.

In your initialize method:

using System.Windows.Forms;

/* ... other methods **/

protected override void Initialize()
    Form form = (Form)Form.FromHandle(this.Window.Handle);

then simply add Controls to that form like:

Panel panel = new Panel() { Width = 200, Dock = DockStyle.Right };

this way, you should have no problem loading anything without problems. It's simply a game that has controls. Much easier than the other way around, because you get no "game loop".

EDIT: You need to create a method that updates the viewport according to the controls when the window is resized (if it ever is) otherwise it will need to be run at least once. Otherwise if you create controls on the form that takes 50% of the viewport, the viewport will still be 100% and positioned wrongly. So, a good idea is to have each control have a "Dock" then you can increase X if it's docked to the left, decrease Width if it's docked to the right, increase Y if it's docked at the top and decrease height if it's docked to the bottom.

I've used this (created an automatic method that gets called everytime the window is resized and when it's initialized) and it sets the viewport correctly and works just fine.

I've only encountered errors while trying to get a Game work in a Windows Form. Much easier the other way around.

I have been using something along that line of thought for another version of this project and definitely agree that it is easier. It was all working well until I realized that there would be a part of the game that would need two areas where images would be displayed via XNA. This is a link to the tutorial that I followed to get as far as I have now: royalexander.wordpress.com/2008/10/09/… It uses XNA to draw to a pictureBox.Handle which is simple enough to do. How would you have two pictureBoxes running with different information displayed on each. –  Daniel Miles May 4 '13 at 19:57
Why do you need to draw it in pictureBoxes? Are you making a game with Controls (Editor?) or a Windows Forms with Graphics support? If you ever made a game, you can just draw them in a viewport or multiple viewports and create controls that can manage whatever you want to manage. –  Deukalion May 4 '13 at 20:33
An example would be that a ComboBox control contains images you load, you change that control's index and that gets the file and creates a Texture2D and draws it on the viewport. You can make the controls send commands to the game. I have no clue why you need to render graphics in a PictureBox if you intend to do a game. Otherwise, you can just convert from Texture2D to Bitmap through a stream to show them or any other way. You haven't really stated a problem except that it draws a red "X" and to my knowledge this is because some object isn't initalized = NULL. –  Deukalion May 4 '13 at 20:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.