Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Using A FileStream As A Texture?

Using A FileStream As A Texture?

Scheduled Pinned Locked Moved C#
csharpdata-structuresdesigngame-devhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SawmillTurtle
    wrote on last edited by
    #1

    I'm still learning C# and I have a long way to go. I wanted to dive into MonoGame because tinkering with game design has been a hobby of mine since I started learning QBasic at the age of 14. I don't want to use their Content Pipeline to load content, because I'd like to figure out how to load custom resources as needed. Someone on Stack Overflow commented HERE that they use FileStreams to load textures directly. I set about trying to figure out how to do that, and I've gotten this far:

    FileStream fs = new FileStream("stars.jpg", FileMode.Open);
    byte[] readString = new byte[fs.Length];
    fs.Read(readString, 0, (int)fs.Length);

    I have no idea if that code even works, because I haven't gotten to a point where running it would yield any visible results. It seems sound, so once I get the file data in the readString array, I need a way to use it as texture data. I tried casting:

    object o = (object)readString;
    background = (Texture2D)o;

    I already know that isn't going to work. I don't even need to run it to figure that one out. I read a little in the MonoGame Reference and thought about using Texture2D.SetData as shown HERE, but I'm not sure how to proceed or even if I'm headed in the right direction. The syntax they provide doesn't really clarify anything.

    public void SetData(
    T[] data
    ) where T : ValueType, new()

    There's an array here, but I'm not sure what it's asking for. Can I provide SetData with the byte array?

    background.SetData(readString);

    Your help would be most appreciated, and it would go a long way towards helping me advance in my learning. Thanks. :) -Turtle

    L 1 Reply Last reply
    0
    • S SawmillTurtle

      I'm still learning C# and I have a long way to go. I wanted to dive into MonoGame because tinkering with game design has been a hobby of mine since I started learning QBasic at the age of 14. I don't want to use their Content Pipeline to load content, because I'd like to figure out how to load custom resources as needed. Someone on Stack Overflow commented HERE that they use FileStreams to load textures directly. I set about trying to figure out how to do that, and I've gotten this far:

      FileStream fs = new FileStream("stars.jpg", FileMode.Open);
      byte[] readString = new byte[fs.Length];
      fs.Read(readString, 0, (int)fs.Length);

      I have no idea if that code even works, because I haven't gotten to a point where running it would yield any visible results. It seems sound, so once I get the file data in the readString array, I need a way to use it as texture data. I tried casting:

      object o = (object)readString;
      background = (Texture2D)o;

      I already know that isn't going to work. I don't even need to run it to figure that one out. I read a little in the MonoGame Reference and thought about using Texture2D.SetData as shown HERE, but I'm not sure how to proceed or even if I'm headed in the right direction. The syntax they provide doesn't really clarify anything.

      public void SetData(
      T[] data
      ) where T : ValueType, new()

      There's an array here, but I'm not sure what it's asking for. Can I provide SetData with the byte array?

      background.SetData(readString);

      Your help would be most appreciated, and it would go a long way towards helping me advance in my learning. Thanks. :) -Turtle

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You cannot get anything meaningful by trying to read a jpg file in that way. Take a look at Image.FromFile Method (System.Drawing) | Microsoft Docs[^].

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups