【问题标题】:How to load Image from user's computer如何从用户的计算机加载图像
【发布时间】:2011-11-03 15:44:12
【问题描述】:

是否可以从用户计算机将图像加载到 XNA 游戏?例如,我想将“C:\Images\Box.png”加载到精灵纹理。是否可以?如果是,怎么做?

【问题讨论】:

    标签: c# image c#-4.0 xna textures


    【解决方案1】:

    在 XNA 4.0 中使用 Texture2D.FromStream

    Texture2D fileTexture;
    using(FileStream fileStream = new FileStream(@"C:\Images\Box.png", FileMode.Open))
    {
        fileTexture = Texture2D.FromStream(GraphicsDevice, fileStream);
    }
    

    如果您在 4.0 之前使用 XNA,那么您可以使用Texture2D.FromFile

    【讨论】:

    • 嗯。我可以通过这种方法从.png文件中加载它吗?
    • 根据文档.gif, .jpg or .png 是支持的,所以是的。
    • 非常感谢!
    • 如果您在没有ContentManager 帮助的情况下像这样加载自己的纹理,您还应该记住您自己负责处理纹理。
    【解决方案2】:
    System.IO.FileStream stream = new System.IO.FileStream(@"C:\Images\Box.png", System.IO.FileMode.Open);
    Texture2D texture = Texture2D.FromStream(GraphicsDevice, stream);
    

    【讨论】:

    • 完成后不要忘记处理流。 (使用stream.Dispose,或我使用过的using 语句)。
    • 你是绝对正确的,我看到你甚至击败了我的解决方案:) +1 来自我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多