2009-12-10 71 views
1

我剛開始學習XNA。這是我作爲一個側面「有趣」項目寫作的第一個程序。將自定義創建的位圖繪製到屏幕

我在繪製創建到屏幕上的位圖時遇到問題。

我知道正在正確地創建位圖,因爲當我運行

bitmap.Save(@"C:\jnk\test.bmp", System.Drawing.Imaging.ImageFormat.Bmp); 

它保存了正確的位圖

我試圖顯示從類繪製函數圖像卻無法得到任何出現。我發現了一些來源使用的System.Drawing.Graphics類來創建位圖,它也顯示它與它

bitmapGraphics.DrawImage(bitmap, new System.Drawing.Point(this.boardXOffset, this.boardYOffset)); 

bitmapGraphics繪製到屏幕是一個Systems.Drawing.Graphics對象和boardOffsets均爲0 。我試圖從一個在我的main函數的draw函數中調用的類中繪製它。

但是我什麼也沒得到,沒有錯誤,也沒有顯示。我會猜測這是因爲它不知道什麼對象可能繪製它?但是我對xna的知識缺乏......任何幫助都會很棒。

如果它可以幫助在所有主要的Program.cs運行此作爲其繪製函數

protected override void Draw(GameTime gameTime) 
{ 
GraphicsDevice.Clear(Color.CornflowerBlue); 
BoardGraphicsEngine.Draw(); 
base.Draw(gameTime); 
} 

,並在BoardGraphicsEngine繪製函數是

public void Draw() 
{ 

int width = Convert.ToInt32(System.Math.Ceiling(board.pixelWidth)); 
int height = Convert.ToInt32(System.Math.Ceiling(board.pixelHeight)); 
width += 1; 
height += 1; 

Bitmap bitmap = new Bitmap(width, height); 
Graphics bitmapGraphics = Graphics.FromImage(bitmap); 
Pen p = new Pen(System.Drawing.Color.Black); 
SolidBrush sb = new SolidBrush(System.Drawing.Color.Black); 

sb = new SolidBrush(board.boardState.backgroundColor); 
bitmapGraphics.FillRectangle(sb, 0, 0, width, height); 

    ... Loop through board and create with a couple calls to 
    bitmapGraphics.FillPolygon(new SolidBrush(board.hexes[i, j].hexState.BackgroundColor), board.hexes[i, j].points); 
    and 
    bitmapGraphics.DrawPolygon(p, board.hexes[i, j].points); 
    and 
    bitmapGraphics.DrawPolygon(p, board.boardState.activeHex.points); 


//bitmap.Save(@"C:\jnk\test.bmp", System.Drawing.Imaging.ImageFormat.Bmp); 

bitmapGraphics.DrawImage(bitmap, new System.Drawing.Point(this.boardXOffset, this.boardYOffset)); 

     bitmapGraphics.Dispose(); 
     bitmap.Dispose(); 
    } 

} 

原始來源大部分是http://www.codeproject.com/KB/graphics/hexagonal_part1.aspx但部分問題可能是這個例子是一個Windows窗體,我正在創建一個xna項目

回答

2

我對XNA知之甚少,但您絕對不會在這裏使用XNA。您正在使用.NET接口訪問GDI +,並且您的Draw()方法將所有內容渲染到屏幕外位圖(bitmapGraphics),除非將屏幕渲染到屏幕緩衝區,否則不會顯示在屏幕上。如果你想學習XNA,我會建議通過XNA特定的教程。

2

嘗試從開始0。你使用的方法不是我認爲的通常的xna方法。