0

I'm trying to change a Drawn rectangle's image

spriteBatch.Draw(screenTexture, screen, background);

Where I reassign the screenTexture based on the image the user selected.

(I do this in update before the first call of draw)

screenTexture = Content.Load<Texture2D>(fileLocation);

However after the file is selected and it gets to the above code it is unable to open the filepath of fileLocation.

Unless there is an easier way I'm trying to solve this by creating a resource with the fileLocation after the user selects it.

I've read something about content pipeline needing to be used and I'm getting lost. Thread code used:

//Thread Method 
Thread newThread = new Thread(new ThreadStart(ThreadMethod));
newThread.SetApartmentState(ApartmentState.STA); 
newThread.Start(); 
public void ThreadMethod() 
{
    OpenFileDialog fileDialog = new OpenFileDialog(); 
    DialogResult result; 
    result = fileDialog.ShowDialog(); 
    if (result == DialogResult.OK) 
    { 
        fileLocation = fileDialog.FileName; 
    }
}   

Answer: Although Sloppy(if someone doesn't mine showing me a cleaner version) This is what gave me the result I wanted.

                    FileStream fileStream = new FileStream(@fileLocation, FileMode.Open, FileAccess.Read);
                    Texture2D myTexture = Texture2D.FromStream(screenTexture.GraphicsDevice, fileStream);

                    screenTexture = myTexture;
  • 1
    What's the path? And what is the error exactly? – Stefan May 24 '14 at 01:50
  • Hi, please put the code in comment somewhere in the question so it is clear to us where it's for. – Stefan May 24 '14 at 01:52
  • @Stefan Error loading "C:\Users\Admin\Desktop\test_pattern.BMP". Cannot open file. – Deluxe_Flame May 24 '14 at 01:53
  • is it really with the: `\....\ ` ? – Stefan May 24 '14 at 01:54
  • No, that was removed for personal reasons, its just the User's log in for windows. It can be interpreted as Admin, I'll edit it. – Deluxe_Flame May 24 '14 at 01:55
  • Did you add the file to resources? – Syed Farjad Zia Zaidi May 24 '14 at 01:57
  • @SyedFarjadZiaZaidi I stated in the main question: "Unless there is an easier way I'm trying to solve this by creating a resource with the fileLocation after the user selects it." I'd like to add the file to resources to fix the problem, but I don't know how. – Deluxe_Flame May 24 '14 at 01:59
  • Right Click on your project -> Add -> Add existing item browse to your file and add it. It will added to your resources. – Syed Farjad Zia Zaidi May 24 '14 at 02:01
  • I have tried experimenting with ResourceWriter, but after rw.AddResource("anImage", img); I try: screenTexture = Content.Load("anImage"); But I think my "anImage" is incorrect. – Deluxe_Flame May 24 '14 at 02:04
  • @SyedFarjadZiaZaidi I need to add the image while the code is running based on the filepath given by the user, not during development. – Deluxe_Flame May 24 '14 at 02:05
  • Once added in your resources you can access it like this Properties.Resources.yourFileName; – Syed Farjad Zia Zaidi May 24 '14 at 02:05
  • Yes right... Did you try it without using another thread? Mostly things related to graphics should be done on the UI thread... – Syed Farjad Zia Zaidi May 24 '14 at 02:07
  • @SyedFarjadZiaZaidi Sorry, I don't know what you mean by another Thread and UI Thread. I'm new to using threads. I appreciate your help in trying to tackle my problem. Could you please re-read all the comments and the main question, so I know we are clear? – Deluxe_Flame May 24 '14 at 02:12
  • http://stackoverflow.com/questions/3495156/how-do-i-load-a-texture-in-xna-at-runtime Read this... I think it will help you. and yes I have read it now, I didn't read it clearly before sorry for that. The UI thread is the main Thread the thread that launches the program. You can create as many thread as you want but there are some processes that need to be used only by the UI thread. There are techniques to work around this problem, synchronization techniques. But that will just be moving away from the topic right now. – Syed Farjad Zia Zaidi May 24 '14 at 02:15
  • Although Sloppy(if someone doesn't mine showing me a cleaner version) This is what gave me the result I wanted. FileStream fileStream = new FileStream(@fileLocation, FileMode.Open, FileAccess.Read); Texture2D myTexture = Texture2D.FromStream(screenTexture.GraphicsDevice, fileStream); screenTexture = myTexture; – Deluxe_Flame May 24 '14 at 02:56

1 Answers1

0
 Although Sloppy(if someone doesn't mine showing me a cleaner version)

this is the

                    FileStream fileStream = new FileStream(@fileLocation, FileMode.Open, FileAccess.Read);
                    Texture2D myTexture = Texture2D.FromStream(screenTexture.GraphicsDevice, fileStream);

                    screenTexture = myTexture;