# Winform + XNA (Microsoft Example) - Issues with Drawing Texture2D

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.Begin();
spriteBatch.Draw(box, new Vector2(0,0),Color.Black);
spriteBatch.End();


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.

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.";

GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin();
spriteBatch.Draw(box, new Vector2(15, 15), Color.Black);
spriteBatch.DrawString(font, message, new Vector2(23, 23), Color.White);
spriteBatch.End();

• 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

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.

using System.Windows.Forms;

/* ... other methods **/

protected override void Initialize()
{
Application.EnableVisualStyles();
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 };