Jump to content
  • Advertisement
Sign in to follow this  

OpenFileDialog not compatible with DirectX, alternatives?

This topic is 4234 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I use C#. I wanted to grant my users the option to select any mp3 on their system and use it as the bg music in my game, so I thought "This'll be easy, give my Game class constructor a string parameter that will be supplied by an OpenFileDialog, and make sure to provide a default mp3 file ofcourse." Except that OpenFileDialog's require the keyword [STAThread] to be placed at the begining of your main class(in my case the frmMain class). This is completely in-compatible with DirectX. I get these errors about some kind of managed code in 3 or 4 random places in the code, one of which is where I instantiate my custom objects(paddle, ball, and an array of blocks). I really like the idea of users being able to play to their favorite songs, do any of you know of any possible alternaties? Remember, the files must be restricted to the mp3 data type because thats the only kind of file DirectSound.AudioVideoPlayback will accept. Thanks for any help in advance.

Share this post

Link to post
Share on other sites
A cursory Google search doesn't reveal any information about an incompatibility between STA Thread and DirectX.. Perhaps you can give more details about the compile-time errors you're having?

The simplest solution would be to simply prompt the user (say, in a simple form with a text box and a label) to enter the path to the MP3 they wish to play. Or for simplicity, require that the mp3 be in a specific directory and prompt the user to enter the filename.

Alternatively you could try to hack OpenFileDialog and see if you can make your own implementation that doesn't require you to use STA Thread.. Only advice I can give you here is that OpenFileDialog is not natively implemented in .NET, it makes use of a Win32 API called GetOpenFileName.

In any case that seems cumbersome and I would focus on getting your program to compile with [STAThread] and use the OpenFileDialog as you originally intended.

Share this post

Link to post
Share on other sites
I have used Managed DirectX with the [STAThread] attribute many many (..many) times [grin] so there are no issues between it and Managed DirectX. So, as steelBovine has said, provide the errors and also any necessary code within [source][/source] tags.


Share this post

Link to post
Share on other sites
This is the error message(which, by the way, only appears if I choose an mp3 that isn't the one I packaged with the game, and isn't the message I remember getting two months ago.)

ArgumentException was Unhandled

Parameter is not Valid

The code I got it in is here:
bmpRedBlock = new Bitmap("redBlock.bmp");

I included the majority of my frmMain code. I left out the form objects (the first elipse) and the auto-generated InitializeComponent(the second elipse).

public class frmMain : Form {


OpenFileDialog ofd;
string soundPath;


public static void Main()
frmMain main = new frmMain();
}//end Main method

public frmMain()
lstLevels.SelectedItem = "1";
ofd = new OpenFileDialog();

ofd.Title = "Select an MP3 file";
ofd.Filter = "MP3 (*.mp3)|*.mp3";

soundPath = Path.Combine(Application.StartupPath, "bgSound.mp3");
txtBgSoundFileName.Text = soundPath;
}//end frmMain method

private void btnStart_Click(object sender, EventArgs e)
int selectedLevel;
selectedLevel = int.Parse(lstLevels.SelectedItem.ToString());

theMenuScreen = this;
GameEngine game = new GameEngine(selectedLevel, soundPath);
game.Text = "BreakOut 2007";

private void btnClose_Click(object sender, EventArgs e)
}// end btnClose_Click

public void setLabel(string text)
lblOutcome.Text = text;
}//end setLabel

private void btnBrowse_Click(object sender, EventArgs e)
if (ofd.ShowDialog() == DialogResult.OK)
soundPath = ofd.FileName;
txtBgSoundFileName.Text = ofd.FileName;
}//end if
}//end btnBrowse_Click
}//end class frmMain

Share this post

Link to post
Share on other sites
It does not seem your error has anything to do with STAThread or OpenFileDialog.

The Bitmap constructor overload Bitmap(string filename), which you are trying to use, will throw a System.ArgumentException iff the stream does not contain image data or is a null reference. So you need to take a look at redBlock.bmp.

I am currently building my first game with C# and XNA (Tetris clone, w00t) and have used OpenFileDialog successfully to get an mp3 filename to play in the background with DirectSound.AudioVideoPlayback. If you would like I can send you my code via PM.

Share this post

Link to post
Share on other sites
That doesn't make any sense. My program works (nearly) flawlessly without this OpenFileDialog feature. I don't even get this problem if I leave the default as is and press start.

Okay, it does compile, maybe I forgot to mention that. If I press Browse, and pick another mp3, the file path appears in the text box as it should. Then I press Start and the music begins, but the program crashes right after and gives me that error.

BUT, if I don't press Browse (just leave it at the default) and just press Start it works fine.

Whats up with that?

P.S. Mine is BreakOut :)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!