public void LoadExternalImages(GraphicsDevice g) { GameState.externalImages = new List <ExternalImage>(); foreach (var file in GetAllExternalImageFiles()) { try { Button button = new Button("cancel-btn", Rectangle.Empty, state.buttons, Color.Red); var fs = new FileStream(file.FullName, FileMode.Open); var tex = Texture2D.FromStream(g, fs); fs.Close(); Color[] colors = new Color[tex.Width * tex.Height]; tex.GetData(colors); ExternalImage img = new ExternalImage { button = button, filename = file.Name, data = colors, w = tex.Width, h = tex.Height }; GameState.externalImages.Add(img); tex.Dispose(); } catch (Exception e) { //do something throw e; } } }
public void Update(GameTime gameTime) { ExternalImage deleteMe = GameState.externalImages.FirstOrDefaultF(img => img.button.WasLeftClicked(state.inputState)); if (deleteMe != null) { File.Delete(assetPath + deleteMe.filename); GameState.externalImages.Remove(deleteMe); } if (addButton.WasLeftClicked(state.inputState)) { using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.Filter = "Image Files(*.JPG;*.JPEG;*.PNG)|*.JPG;*.JPEG;*.PNG"; openFileDialog.FilterIndex = 0; openFileDialog.RestoreDirectory = false; if (openFileDialog.ShowDialog() == DialogResult.OK) { //Get the path of specified file var filePath = openFileDialog.FileName; var name = filePath.Substring(filePath.LastIndexOf("\\")); File.Copy(filePath, assetPath + name); LoadExternalImages(state.g); } } } if (closeButton.WasLeftClicked(state.inputState)) { state.screen = Screen.CHOOSE; } }