简体   繁体   English

Texture2D渲染图像

[英]Texture2D render image

What is the fastest possible way to fill a Texture2D with image? 使用图像填充Texture2D的最快方法是什么?

Under /Images/image.png I have the desired image. 在/Images/image.png下我有所需的图像。

My existing code just fills the texture with white color. 我现有的代码只用白色填充纹理。 How can I make it render the image? 如何让它渲染图像?

Texture2D texture = new Texture2D(graphics.GraphicsDevice, this.Width, this.Height, false, SurfaceFormat.Color);
Color[] colors = new Color[this.Width * this.Height];
for (int j = 0; j < colors.Length; j++)
    colors[j] = color;
texture.SetData(colors);

Maybe I do it the wrong way but what is the easiest/fastest way to dynamicly display image on the screen? 也许我做错了但是在屏幕上动态显示图像的最简单/最快的方法是什么?

Update 更新

I changed my code to use ContentManager. 我更改了代码以使用ContentManager。

ContentManager contentManager = (Application.Current as App).Content;
Texture2D texture = contentManager.Load<Texture2D>("/Images/block.png");

contentManager.Load<Texture2D>(@"Images/block.png")

throws "File not found". 抛出“未找到文件”。

contentManager.Load<Texture2D>(@"/Images/block.png")

throws "Error loading "\\Images\\block.png". Cannot open file." 抛出“加载错误”\\ Images \\ block.png“。无法打开文件。”

The easiest possible way is to just rely on the content pipeline as suggested. 最简单的方法是按照建议依赖内容管道。

When you create your XNA project, you should have two projects: MyGame, and MyGame (Content). 在创建XNA项目时,您应该有两个项目:MyGame和MyGame(Content)。 Right click on the Content and select "Add Files". 右键单击“内容”,然后选择“添加文件”。 This will allow you to import images that can easily be pathed and get compiled into a .xnb format. 这将允许您导入可以轻松修改并编译为.xnb格式的图像。 I believe this is intended to accelerate things a little. 我相信这是为了加速一点事情。

Once you've done that, you need a ContentManager object. 完成后,您需要一个ContentManager对象。 Ideally you will use Content from your game class - which is already set up for you. 理想情况下,您将使用游戏类中的Content - 这已经为您设置了。 But you can create one with: 但你可以创建一个:

ContentMananger content = new ContentManager(Services, "Content");

Where Services is from your game class (or an IServiceProvider with a GraphicsDeviceService ). Services来自您的游戏类(或具有GraphicsDeviceServiceIServiceProvider )。 The second argument is the root directory of your content folder. 第二个参数是内容文件夹的根目录。

Which will allow you to load arbitrary data formats from the content pipeline via: 这将允许您通过以下方式从内容管道加载任意数据格式:

content.Load<Texture2D>("myTexture");

(Note that you do not include the file extension.) (请注意,您不包含文件扩展名。)

For ease, I suggest you make an "Images" folder in the Content project as you will have a large number of resources eventually and it makes it easier to organize. 为方便起见,我建议您在Content项目中创建一个“Images”文件夹,因为最终会有大量资源,这样可以更轻松地进行组织。 You could also have an SFX, Music, XML, etc, folders to handle all the data types your game will need. 您还可以使用SFX,音乐,XML等文件夹来处理游戏所需的所有数据类型。

So you'd do: 所以你要这样做:

Texture2D myTexture = content.Load<Texture2D>("images/myTexture");
SpriteBatch spriteBatch = new SpriteBatch(graphics.GraphicsDevice);

(Note that these objects obviously need to be visible at the class scope). (请注意,这些对象显然需要在类范围内可见)。 This would occur in your Initialization code for loading files. 这将出现在用于加载文件的初始化代码中。

After that, you would use the Draw method to actually draw the sprite 之后,您将使用Draw方法实际绘制精灵

public override void Draw(SpriteBatch spriteBatch)
{
   spriteBatch.Begin();
   spriteBatch.Draw(myTexture, new Vector2(320, 240), Color.White);
   spriteBatch.End();
}

And your sprite is magically drawn. 而你的精灵是神奇地画出来的。

What you're doing is essentially programmatically defining an image and then drawing it. 你正在做的主要是以编程方式定义一个图像,然后绘制它。 While they has applications at points, probably not what you're looking for. 虽然他们有点应用,可能不是你想要的。

If you're trying to load an image at runtime, you can use this piece of code: 如果您尝试在运行时加载图像,则可以使用以下代码:

string file = @"Content/images/1.png"; // @ sign is to treat '/' as a character, not a control symbol
FileStream fs = new FileStream(file, FileMode.Open);
Texture2D texture = Texture2D.FromStream(GraphicsDevice, fs);
fs.Dispose();

…And then just use texture like so: ...然后只使用这样的texture

spritebatch.Draw(texture, new Vector2(123, 456), Color.White);

To use FileStream you may need to add using System.IO; 要使用FileStream您可能需要using System.IO;添加using System.IO; in your game class. 在你的游戏课上。

This method is not considered standard practice, it may lead to problems with alpha channel, and to fix them you'll need to use a different alpha blending state. 此方法不被视为标准做法,可能会导致alpha通道出现问题,要修复它们,您需要使用不同的Alpha混合状态。 You can find more on that in this question . 您可以在此问题中找到更多相关信息。

If you're not using the content pipeline to add the texture, use Texture2D.FromStream() : 如果您没有使用内容管道添加纹理,请使用Texture2D.FromStream()

FileInfo imageFile = new FileInfo("Images\\image.png");
Texture2D image = Texture2D.FromStream(GraphicsDevice, imageFile.OpenRead());

If you're loading a lot of textures, you might want to dispose them when you don't need them anymore . 如果您正在加载大量纹理,则可能需要在不再需要它们时处置它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM