0

XNA 程序是否有可能通过代码将资源(等:图像、声音)导入程序内容?例如,用户想在程序中添加一个新图像,XNA 程序的行为就像 Window 的 Add File 或 Copy File。如果可能,WinForm 应避免。

4

1 回答 1

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 Window,但如果您不想要 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);
        }
于 2013-02-05T12:40:58.620 回答