使用代码将内容导入XNA程序?

时间:2013-02-05 01:45:01

标签: xna

XNA程序是否有可能通过代码将资源(etc:image,sound)导入到程序内容中?例如,用户想要在程序中添加新图像,XNA程序的行为类似于Window的添加文件或复制文件。如果可能,WinForm应避免。

1 个答案:

答案 0 :(得分:1)

         OpenFileDialog f = new OpenFileDialog();
         f.Filter = "PNG files (*.png)|*.png|All files (*.*)|*.*";
         f.Title = "Import Image"; 
         DialogResult result = f.ShowDialog(); // Show the dialog.   
         string file = "";
         if (result == DialogResult.OK) // Test result.
         {
            file = f.FileName;
         }
         else //If cancels, handle here
         Application.Exit();
         using (FileStream SourceStream = File.Open(file, FileMode.Open))
         {
             //Load the Texture here
             YourTexture = Texture2D.FromStream(GraphicsDevice, SourceStream);
         }

它使用一个简单的WinForms OpenDialog窗口,但是如果你不想要winforms,你可以自己制作并使用这个部分来加载。

       using (FileStream SourceStream = File.Open(file, FileMode.Open))
       {
           //Load the Texture here
           YourTexture = Texture2D.FromStream(GraphicsDevice, SourceStream);
       }

您可以通过

保存Texture2D Back
        using(Stream stream = File.Create(file)); 
        {
        texture.SaveAsPng(stream, texture.Width, texture.Height);
        }