2013-01-01 90 views
-1

我正在尝试在一个纹理上绘制很多纹理以创建RTS游戏的地图,同时我可以在屏幕上绘制个别纹理,并将它们全部绘制到渲染目标似乎没有效果(调试时窗口保持AliceBlue)。我试图确定是否有任何东西甚至被绘制到渲染目标,所以我试图将它保存为Jpeg到一个文件,然后从我的桌面查看该Jpeg。我如何从MemoryStream访问Jpeg?如何将texture2D保存为jpeg文件

保护覆盖无效LoadContent(){

  spriteBatch = new SpriteBatch(GraphicsDevice); 
     gridImage = new RenderTarget2D(GraphicsDevice, 1000, 1000); 
     GraphicsDevice.SetRenderTarget(gridImage); 
     GraphicsDevice.Clear(Color.AliceBlue); 
     spriteBatch.Begin(); 


     foreach (tile t in grid.tiles) 
     { 
      Texture2D dirt = Content.Load<Texture2D>(t.texture); 
      spriteBatch.Draw(dirt, t.getVector2(), Color.White); 
     } 
     test = Content.Load<Texture2D>("dirt"); 


      GraphicsDevice.SetRenderTarget(null); 
      MemoryStream memoryStream = new MemoryStream(); 

      gridImage.SaveAsJpeg(memoryStream, gridImage.Width, gridImage.Height); //Or SaveAsPng(memoryStream, texture.Width, texture.Height) 



     // rt.Dispose(); 
      spriteBatch.End(); 
    } 
+1

@Skami你不应该[添加冗余标签的问题](http://meta.stackexchange.com/questions/99072/should-adding-redundant-but-related-tags-be-encouraged-或-泄气)。您今天已经完成了[超过200个微小的修改](http://meta.stackexchange.com/questions/164503/gaming-the-edit-system-with-tiny-edits),其中大多数并没有真正提高质量的网站。 – Lucius

+0

@Lucius对不起,我会更彻底地阅读规则,并确保我不会再搞乱。 – Skami

+1

@Skami我相信你是用最好的意图去做的。请记住,每次你进行这样的编辑时,至少有3位版主必须处理它,并且问题会被碰撞。另外,XNA并不一定意味着C#。 XNA在理论上适用于任何.NET语言,并且至少在C#*和* VB中是实用的。 – Lucius

回答

2

我做了一个简单的截图方法,

void Screenie() 
      { 
       int width = GraphicsDevice.PresentationParameters.BackBufferWidth; 
       int height = GraphicsDevice.PresentationParameters.BackBufferHeight; 

       //Force a frame to be drawn (otherwise back buffer is empty) 
       Draw(new GameTime()); 

       //Pull the picture from the buffer 
       int[] backBuffer = new int[width * height]; 
       GraphicsDevice.GetBackBufferData(backBuffer); 

       //Copy to texture 
       Texture2D texture = new Texture2D(GraphicsDevice, width, height, false, GraphicsDevice.PresentationParameters.BackBufferFormat); 
       texture.SetData(backBuffer); 
       //Get a date for file name 
       DateTime date = DateTime.Now; //Get the date for the file name 
       Stream stream = File.Create(SCREENSHOT FOLDER + date.ToString("MM-dd-yy H;mm;ss") + ".png"); 

       //Save as PNG 
       texture.SaveAsPng(stream, width, height); 
       stream.Dispose(); 
       texture.Dispose(); 
      } 

而且,你加载Texture2D dirt = Content.Load<Texture2D>(t.texture);每一帧?它看起来像...不这样做!这将导致大量的延迟加载数百瓦,每秒数百次!而不是做一个全局纹理Texture2D DirtDexture并在LoadContent()方法做DirtTexture = Content.Load<Texture2D>(t.texture);现在,当你画,你可以做spriteBatch.Draw(DirtTexture,...

做同样的spriteBatch = new SpriteBatch(GraphicsDevice);gridImage = new RenderTarget2D(GraphicsDevice, 1000, 1000);

你不需要做出新的渲染目标和Spritebatch每一帧!只需在Initialize()方法中完成!

另见RenderTarget2DXNA RenderTarget Sample欲了解更多有关使用渲染目标

编辑:我知道一切都在LoadContent,我没有看到,因为该格式被搞砸了,记得要加你的foreach(瓷砖等)在你的绘制方法

+0

谢谢!实际上,我正在将所有这些图块绘制到渲染目标上,以便我可以避免在每一帧中绘制它们:我只想将它们绘制到一个大的位图上,然后只绘制该位图。我发现这个截图方法很有用,因为它向我展示了Stream是如何工作的。如果我想将图像保存到桌面,我只需将SCREENSHOT FOLDER更改为其他路径名称,对不对? –

+0

正确,它会将其保存在您指定的文件夹中,并使用当前日期/时间作为文件名。 – Cyral

相关问题