如何创建精灵图像

sto*_*oic 7 c# image-processing winforms

我正在尝试创建一个非常基本的精灵图像.

首先,我有一个现有的图像(宽度= 100像素,高度= 100像素).

我将循环显示此图像10到100次,每次将它放在前一个旁边的精灵上.

精灵限制在3000px宽.

将图像放在一起是很好的,因为我可以将它们与一个简单的方法结合起来,但是,我需要将组合图像的宽度限制为3000px,然后从一个新行开始.

Mat*_*att 7

以下MSDN文章中有关于2D精灵的大量信息:渲染2D精灵

这些示例基于Microsoft的XNA,这是一个可以在Visual Studio中用于开发Windows,Windows Phone和XBOX 360游戏的平台.

例如,要绘制精灵,您可以使用以下C#代码(示例取自MSDN文章,删除XBOX 360特定代码):

private Texture2D SpriteTexture;
private Rectangle TitleSafe;

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
        SpriteTexture = Content.Load<Texture2D>("ship");
        TitleSafe = GetTitleSafeArea(.8f);
    }

    protected Rectangle GetTitleSafeArea(float percent)
    {
        Rectangle retval = new Rectangle(
            graphics.GraphicsDevice.Viewport.X,
            graphics.GraphicsDevice.Viewport.Y,
            graphics.GraphicsDevice.Viewport.Width,
            graphics.GraphicsDevice.Viewport.Height);
        return retval;
    }

    protected override void Draw(GameTime gameTime)
    {
        graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        Vector2 pos = new Vector2(TitleSafe.Left, TitleSafe.Top);
        spriteBatch.Draw(SpriteTexture, pos, Color.White);
        spriteBatch.End();
        base.Draw(gameTime);
    }
Run Code Online (Sandbox Code Playgroud)

你需要调用LoadContent()初始化它,然后你需要调用GetTitleSafeArea(100)以获得安全绘制区域(在这种情况下是100%),最后你可以使用该Draw方法.它接受包含GameTime类实例的参数,该实例是游戏计时状态的快照,以可由变步(实时)或固定步(游戏时间)游戏使用的值表示.

如果有帮助,请告诉我.

  • 嗨Jeremy,我同意,因此我添加了更多细节.问候,马特 (3认同)
  • 嗨Matt,StackOverflow中的习惯是回答包括链接内容的摘要或专门回答问题的重点.SE网站的目标是在未来几年内成为知识和答案的资源.通过仅链接答案,操作系统必须挖掘另一个资源以找到他/她可能不确定的答案.最重要的是,如果您的链接永远中断(通常是微软的链接随着时间推移),那么您的答案对于将来访问此页面的任何人来说都是无用的.考虑制作并编辑您的答案以添加更多详细信息.祝好运! (2认同)

Dan*_*dor 3

让我尝试一些伪代码:

Bitmap originalImage; //  that is your image of 100x100 pixels
Bitmap bigImage;      //  this is your 3000x3000 canvas
int xPut = 0;
int yPut = 0;
int maxHeight = 0;
while (someExitCondition) 
{
    Bitmap imagePiece = GetImagePieceAccordingToSomeParameters(originalImage);
    if (xPut + imagePiece.Width > 3000)
    {
        xPut = 0;
        yPut += maxHeight;
        maxHeight = 0;
    }
    DrawPieceToCanvas(bigImage, xPut, yPut, imagePiece);
    xPut += imagePiece.Width;
    if (imagePiece.Height > maxHeight) maxHeight = imagePiece.Height;
    //  iterate until done
}
Run Code Online (Sandbox Code Playgroud)