0

XNAプログラムがリソース(画像、音声など)をコードを介してプログラムのコンテンツにインポートできる可能性はありますか?たとえば、ユーザーがプログラムに新しい画像を追加したい場合、XNAプログラムはウィンドウのファイルの追加またはファイルのコピーのように動作します。可能であれば、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);
         }

これは単純なWinFormsOpenDialogWindowを使用しますが、winformsが必要ない場合は、独自に作成して、この部分をロードするためだけに使用できます。

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

あなたはすることによってTexture2Dを保存することができます

        using(Stream stream = File.Create(file)); 
        {
        texture.SaveAsPng(stream, texture.Width, texture.Height);
        }
于 2013-02-05T12:40:58.620 に答える