title: XNA with Windows Forms - Opening new window
body: <p>Okay, so here we go. I'm attempting to make an application, using XNA as the base because of its renderer. One of the things that is necessary in this project is to open a new window (as a dialog), in which is embedded a separate XNA render panel. I'm using this as an interactive preview panel, so I absolutely need XNA to render in there. However, it seems XNA is not very well equipped to do this. I have tried various things myself, but to no avail (either producing errors and not rendering correctly, or rendering in the wrong aspect ratio, etc.). Normally, I would post code here, but since I have had such little luck, there isn't much to post.</p>
  
  <p>My application currently consists of an XNA application embedded within a Form, and I have a button to open the preview panel, which in theory should pop up as a Form Dialog, containing the XNA renderer, to allow me to draw the preview. I have been trying this for several hours, and got nowhere, so I'm asking for a bit of help here.</p>
  
  <p>Thanks, anyway.</p>
  
  <p>EDIT: Okay, I've made a little progress, but I have 2 problems. Firstly, any textures drawn with a sprite batch appear the right dimensions, but filled with solid black. Also, when I open the dialog, and then close it, and close the application, I get an AccessViolationException. I strongly suspect the two errors are linked in some way.</p>
  
  <p>Here is my code initialising the preview dialog. (a is a custom class that essentially consists of a LinkedList of Texture2D objects).</p>
  
  <pre><code> animPrev = new AnimationPreview(a);
   animPrev.Show();
  </code></pre>
  
  <p>My AnimationPreview class is an extension of the Form class, and contains a PreviewControl object, which is an extension of the GraphicsDeviceControl found in the XNA Winforms sample. Note that my main form extends the XNA Game class, for various reasons.</p>
  
  <p>The PreviewControl object is set up like this:</p>
  
  <pre><code>protected override void Initialize()
  {
      sb = new SpriteBatch(GraphicsDevice);
      Application.Idle += delegate { Invalidate(); };
  }
  </code></pre>
  
  <p>And the Draw method contains:</p>
  
  <pre><code>protected override void Draw()
  {
      GraphicsDevice.Clear(Microsoft.Xna.Framework.Graphics.Color.Violet);
      if (frame != null)
      {
          sb.Begin();
          sb.Draw(Image, Vector2.Zero, Color.White);
          sb.End();
      }
   }
  </code></pre>
  
  <p>This clears the background of the form violet, as expected, and draws a black box of the same size as Image. This is not expected. Hopefully someone can help me out here.</p>
  
  <p>NOTE: An acceptable alternative would be to convert XNA Texture2D objects to System.Drawing.Image objects. However, I am using XNA 3.1, so I can't just save the texture to a stream and reload it.
  Actually, after having tried this, it's a bit dodgy, and very slow, so I'd rather not do it this way.</p>