2013-01-01 96 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

相關問題