Sunday, June 7, 2015

c# - 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:


http://imgur.com/lJT0HrW


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.


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

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();

Answer



you are throwing an exception...


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


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...