开发者

Drawing a custom created bitmap to screen

开发者 https://www.devze.com 2022-12-14 03:04 出处:网络
I have just started learning XNA. This is my first program that I am writing as a side \"fun\" project.

I have just started learning XNA. This is my first program that I am writing as a side "fun" project.

I am having trouble drawing a bitmap that gets created to the screen.

I know the bitmap is being created correctly because when I run

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

it saves the correct bitmap

I am trying to display the image from a class draw function however cannot get anything to appear. I found some sources that used a System.Drawing.Graphics class to create the bitmap and it also showed it drawing it to the screen with

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

bitmapGraphics is a Systems.Drawing.Graphics object and the boardOffsets are both 0. I am trying to draw this from a class which is called in the draw function of my main.

However I get nothing, No errors, and no display. I am going to guess that this is because It does not know what object to draw it onto perhaps? B开发者_如何学Pythonut my lack of knowledge in xna... any help would be great.

if it helps at all the main program.cs runs this as its draw function

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

and the draw function in the BoardGraphicsEngine is

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();
    }

}

The original source for most of this is http://www.codeproject.com/KB/graphics/hexagonal_part1.aspx but part of the problem may be this example was a windows form and i'm creating an xna project


I know very little about XNA, but you're definitely not using XNA here. You're using the .NET interface to GDI+, and your Draw() method is rendering everything to an off-screen bitmap (bitmapGraphics), which isn't going to show up on screen unless you render it to the screen buffer. If you want to learn XNA, I would recommend working through an XNA-specific tutorial.


Try starting with the xna 2d tutorial. The method you are using is not the usual xna method I think.

0

精彩评论

暂无评论...
验证码 换一张
取 消