XNA Creators Club Online
Page 1 of 1 (8 items)
Sort Posts: Previous Next

Problems creating a Texture2D from a HTTP stream

Last post 11/20/2009 3:04 AM by David Hunt. 7 replies.
  • 10/22/2008 11:43 AM

    Problems creating a Texture2D from a HTTP stream

    I am trying to load some images via HTTP streams into a Texture2D.

    The code I have written uses a HttpWebRequest to get a Stream from a URL. It then reads that Stream into a MemoryStream, and passes the MemoryStream into Texture2D.FromFile which keeps chucking out an InvalidOperationException:

    System.InvalidOperationException was unhandled
      Message="An unexpected error has occurred."
      Source="Microsoft.Xna.Framework"
      StackTrace:
           at Microsoft.Xna.Framework.Graphics.Texture.GetTextureInformation(String filename)
           at Microsoft.Xna.Framework.Graphics.Texture.FromFile(GraphicsDevice graphicsDevice, String filename)
           at Microsoft.Xna.Framework.Graphics.Texture.FromFile(GraphicsDevice graphicsDevice, Stream textureStream, Int32 numberBytes)
           at Microsoft.Xna.Framework.Graphics.Texture2D.FromFile(GraphicsDevice graphicsDevice, Stream textureStream)
           at XNAVirtualEarth.WebImage..ctor(Game game, String uri) in C:\Documents and Settings\Leith Bade\My Documents\Visual Studio 2008\Projects\XNAVirtualEarth\XNAVirtualEarth\WebImage.cs:line 38
           at XNAVirtualEarth.Game1.LoadContent() in C:\Documents and Settings\Leith Bade\My Documents\Visual Studio 2008\Projects\XNAVirtualEarth\XNAVirtualEarth\Game1.cs:line 51
           at Microsoft.Xna.Framework.Game.Initialize()
           at XNAVirtualEarth.Game1.Initialize() in C:\Documents and Settings\Leith Bade\My Documents\Visual Studio 2008\Projects\XNAVirtualEarth\XNAVirtualEarth\Game1.cs:line 39
           at Microsoft.Xna.Framework.Game.Run()
           at XNAVirtualEarth.Program.Main(String[ args) in C:\Documents and Settings\Leith Bade\My Documents\Visual Studio 2008\Projects\XNAVirtualEarth\XNAVirtualEarth\Program.cs:line 14
      InnerException:

    Here is the code that creates the Texture2D:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);  
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
    Stream webStream = response.GetResponseStream();  
     
    Stream cachedStream = CacheStream(webStream);  
     
    webStream.Close();  
    response.Close();  
     
    texture = Texture2D.FromFile(Game.GraphicsDevice, cachedStream);  
     
    cachedStream.Close(); 

    Here is CacheStream:

    Stream CacheStream(Stream stream)  
    {  
        Stream cachedStream = new MemoryStream();  
     
        const int bufferSize = 1500;  
        byte[ buffer = new byte[bufferSize];  
     
        int read = 0;  
     
        while ((read = stream.Read(buffer, 0, bufferSize)) != 0)  
        {  
            cachedStream.Write(buffer, 0, read);  
        }  
     
        return cachedStream;  

    I have tried with PNGs (with and without transparency) and JPEG, none of which work.

    Someone here must know whats going on.

  • 10/22/2008 2:44 PM In reply to

    Re: Problems creating a Texture2D from a HTTP stream

    Try resetting the memory stream after you have finished populating it. The load is probably getting an end-of-stream error as soon as it attempts to read the texture in.

    Stream CacheStream(Stream stream)
    {
       Stream cachedStream = new MemoryStream();

       const int bufferSize = 1500;
       byte[ buffer = new byte[bufferSize];

       int read = 0;

       while ((read = stream.Read(buffer, 0, bufferSize)) != 0)
      {
         cachedStream.Write(buffer, 0, read);
       }

       cachedStream.Seek(0, SeekOrigin.Begin);

       return cachedStream;
    }
  • 10/22/2008 10:09 PM In reply to

    Re: Problems creating a Texture2D from a HTTP stream

    Thanks, that did the trick.
  • 11/19/2009 6:26 PM In reply to

    Re: Problems creating a Texture2D from a HTTP stream

    I did the same thing, including the seek to 0 and it didn't work for me. I'm trying to load from..
    "http://www.RedstingOnline.com/Images/Textures/Grass/0.png"
    32x32 png with transparency.
    And ideas?

    System.InvalidOperationException was unhandled
      Message="An unexpected error has occurred."
      Source="Microsoft.Xna.Framework"
      StackTrace:
           at Microsoft.Xna.Framework.Graphics.Texture.GetTextureInformation(String filename)
           at Microsoft.Xna.Framework.Graphics.Texture.FromFile(GraphicsDevice graphicsDevice, String filename)
           at Microsoft.Xna.Framework.Graphics.Texture.FromFile(GraphicsDevice graphicsDevice, Stream textureStream, Int32 numberBytes)
           at Microsoft.Xna.Framework.Graphics.Texture2D.FromFile(GraphicsDevice graphicsDevice, Stream textureStream)
           at XNA_GameEngine.Texture.getTexture(GraphicsDevice device, String url) in C:\Documents and Settings\Redsting\Desktop\Independent Study Project\XNA_GameEngine\XNA_GameEngine\Texture.cs:line 74
           at XNA_GameEngine.Texture.Initialize(GraphicsDevice device, ContentManager Content) in C:\Documents and Settings\Redsting\Desktop\Independent Study Project\XNA_GameEngine\XNA_GameEngine\Texture.cs:line 47
           at XNA_GameEngine.Game1.LoadContent() in C:\Documents and Settings\Redsting\Desktop\Independent Study Project\XNA_GameEngine\XNA_GameEngine\Game1.cs:line 128
           at Microsoft.Xna.Framework.Game.Initialize()
           at XNA_GameEngine.Game1.Initialize() in C:\Documents and Settings\Redsting\Desktop\Independent Study Project\XNA_GameEngine\XNA_GameEngine\Game1.cs:line 103
           at Microsoft.Xna.Framework.Game.Run()
           at XNA_GameEngine.Program.Main(String[] args) in C:\Documents and Settings\Redsting\Desktop\Independent Study Project\XNA_GameEngine\XNA_GameEngine\Program.cs:line 14
      InnerException:

  • 11/19/2009 6:38 PM In reply to

    Re: Problems creating a Texture2D from a HTTP stream

    You'll need to post your texture loading code before we can help you.
  • 11/19/2009 6:42 PM In reply to

    Re: Problems creating a Texture2D from a HTTP stream

    Texture2D.FromFile does strange things when creating from an existing stream, namely it creates a temp file, writes out the stream data to that file, and then loads from that file (as if you had called FromFile(path)), and deletes the file.

    It sounds like you get to the stage where the temp if is written out, but in your case when it tries to determine the texture format by reading back in the data it fails. So I'd guess that either you don't have permission on the temp file (unlikely) or your stream data contains something other than raw image data (maybe HTTP headers, maybe the wrong encoding, etc). How are you making the web request and filling the memory stream?
  • 11/19/2009 10:41 PM In reply to

    Re: Problems creating a Texture2D from a HTTP stream

    I'm running almost the exact same thing. Here's a snippet from my calling function.
    It works if I use "localhost", or the IP address ("74.69.137.222" rather than "www.RedstingOnline.com" running a basic server app, which is really confusing me. And as you can see, my site is at least functional.

    Texture2D[] temp = new Texture2D[9]; 
    for (byte i = 0; i < 9; i++) 
        temp[i] = getTexture(device, "http://www.RedstingOnline.com/Images/Textures/Grass/" + i.ToString() + ".png"); 

    And here is the code i derived from above. (NOTE: I am using this all to build a static list within a static class)

            private static Texture2D getTexture(GraphicsDevice device, String url) 
            { 
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
                HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
                Stream webStream = response.GetResponseStream(); 
     
                Stream cachedStream = CacheStream(webStream); 
     
                webStream.Close(); 
                response.Close(); 
     
                Texture2D texture = Texture2D.FromFile(device, cachedStream); 
     
                cachedStream.Close(); 
                return texture; 
            } 
     
            private static Stream CacheStream(Stream stream) 
            { 
                Stream cachedStream = new MemoryStream(); 
     
                const int bufferSize = 1500; 
                byte[] buffer = new byte[bufferSize]; 
     
                int read = 0; 
     
                while ((read = stream.Read(buffer, 0, bufferSize)) != 0) 
                { 
                    cachedStream.Write(buffer, 0, read); 
                } 
     
                cachedStream.Seek(0, SeekOrigin.Begin); 
     
                return cachedStream; 
            } 
     

  • 11/20/2009 3:04 AM In reply to

    Re: Problems creating a Texture2D from a HTTP stream

    It appears that when you access your png file via the DNS name, your web server is adding HTML to wrap the image in a frame. When you go directly via the IP address, the wrapping doesn't occur. So, your code is failing because the stream doesn't just contain a png file. It is actually an HTML file.

Page 1 of 1 (8 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG